糟糕。刚刚尝试过。显然我用点符号太多了,并没有意识到它做了多少。 :P
#import "NSObject.h"
#include <stdio.h>
@interface Test : NSObject
@property (getter=myStuff) int stuff;
@end
@implementation Test
-(int)myStuff { return 42; }
-(void)setStuff:(int)value { /* don't care */ }
@end
int main() {
@autoreleasepool {
Test* test = [[Test alloc] init];
/* All these work... */
printf("test.stuff == %d\n", test.stuff);
printf("[test myStuff] == %d\n", [test myStuff]);
printf("test.myStuff == %d\n", test.myStuff);
/* but here, there's an exception */
printf("[test stuff] == %d\n", [test stuff]);
return 0;
}
}
当我编译这个(使用铛在Linux中),有关于丢失-(int)stuff
的奇怪两次警告。输出看起来像
[email protected]:~/code/objc$ ./a.out
test.stuff == 42
[test myStuff] == 42
test.myStuff == 42
: Uncaught exception NSInvalidArgumentException, reason: -[Test stuff]: unrecognized selector sent to instance 0x2367f38
[email protected]:~/code/objc$
所以,恩,是的。忽略一半以下的东西。 :P
self.working
仅仅是[self working]
语法糖(或[self setWorking:value]
如果你分配给它)。任何一个都会执行相同的操作:返回[self isWorking]
的值,因为这是您定义的吸气剂。
如果你想避开吸气剂,试试_working
或self->_working
(或任何你命名的伊娃)。否则,self.working
,[self working]
,和[self isWorking]
(甚至如果你感觉很勇敢的话self.isWorking
)都应该给你相同的结果。
类似:http://stackoverflow.com/questions/9220028/dot-syntax-vs-method-syntax-with-getter。 –