我们知道在objective-C 语言中,支持property的方式来为变量声明getset方法,还有synthesize的方式为变量完成getset方法。那么当省略synthesize的时候,或者synthesize的时候指明操作的变量时等。情况又是怎么样呢?
@interface Test :NSobject {
NSString *str;
}
@property (nonatomic,retain)NSString str;
@end
@implementation Test
像这种情况,实际上Test类有两个变量,一个str ,还有一个_str,后者是有系统自动产生。
@interface Test :NSobject {
NSString *str;
}
@property (nonatomic,retain)NSString str;
@end
@implementation Test
@synthesize str;
这种情况只有str一个变量。
@interface Test :NSobject {
}
@property (nonatomic,retain)NSString str;
@end
@implementation Test
这种情况也只有str一个变量。
@interface Test :NSobject {
}
@property (nonatomic,retain)NSString str;
@end
@implementation Test
<pre name="code" class="objc">@synthesize str = _str;
这样的是Test有一个_str变量
所以写property ,synthesize以及定义类变量时候,要注意一下,以免操作错误的类变量