Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios getter和Setter不工作目标c_Ios_Objective C_Ios4 - Fatal编程技术网

Ios getter和Setter不工作目标c

Ios getter和Setter不工作目标c,ios,objective-c,ios4,Ios,Objective C,Ios4,我不能在目标c中这样做吗 @interface Foo : NSObject { int apple; int banana; } @property int fruitCount; @end @implementation Foo @synthesize fruitCount; //without this compiler errors when trying to access fruitCount -(int)getFruitCount {

我不能在目标c中这样做吗

@interface Foo : NSObject {
     int apple;
     int banana;         
}

@property int fruitCount;
@end

@implementation Foo
@synthesize fruitCount; //without this compiler errors when trying to access fruitCount

-(int)getFruitCount {
      return apple + banana;
}

-(void)setFruitCount:(int)value {
      apple = value / 2;
      banana = value / 2;
}

@end
我是这样使用这个类的:

Foo *foo = [[Foo alloc] init];
foo.fruitCount = 7;
然而,我的能手和二传手并没有接到电话。如果我改写:

 @property (getter=getFruitCount, setter=setFruitCount:) int fruitCount;

我的接球手被召唤,但是二传手仍然没有被召唤。我遗漏了什么?

您的语法有点不正确。。。 要在示例中定义自己的属性访问器实现,请使用以下命令:

@implementation Foo
@dynamic fruitCount;

-(int)fruitCount {
   return apple + banana;
}
-(void)setFruitCount:(int)value {
      apple = value / 2;
      banana = value / 2;
}

@end

使用@synthesis会告诉编译器生成默认访问器,在本例中,您显然不希望使用默认访问器@动态向编译器指示您将编写它们。苹果的文档中曾经有一个很好的例子,但不知怎的,它在他们的4.0SDK更新中被破坏了。。。希望有帮助

事实证明,我的setter名称中有一个输入错误,直到我将其设置为@dynamic并得到一个sigbart,我才找到它,因为我缺少setter方法。再次感谢。