[iphone SDK] nonatomic,readonly,retain,readwrite....的理解
1.readonly表示这个属性是只读的,就是只生成getter方法,不会生成setter方法.
2.readwrite,设置可供访问级别
3.retain,是说明该属性在赋值的时候,先release之前的值,然后再赋新值给属性,引用再加1。
4.nonatomic,非原子性访问,不加同步,多线程并发访问会提高性能。注意,如果不加此属性,则默认是两个访问方法都为原子型事务访问。
@property(nonatomic, retain) NSString *text; translates as
"I have a member variable of type NSString* named 'text'. I will want a getter/setter pair that uses the retain/release procedure."
@property(nonatomic, assign) int value; translates as
"I have a member variable of type int named 'value'. I will want a getter/setter pair that doesn't use the retain/release procedure - just assign it."
加上一段参考代码。。。
- (UITextField *) userName {
return userName;
}
- (void) setUserName:(UITextField *)userName_ {
[userName release];
userName = [userName_ retain];
}
@property(retain) UITextField *userName自动生成的代码
- (UITextField *) userName {
UITextField *retval = nil;
@synchronized(self) {
retval = [[userName retain] autorelease];
}
return retval;
}
- (void) setUserName:(UITextField *)userName_ {
@synchronized(self) {
[userName release];
userName = [userName_ retain];
}
}
参考资料:http://www.cocoachina.com/bbs/read.php?tid-10663-keyword-%40property.html
作者:Alexliu(alex dotNet Learning)
出处:http://alexliu.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,转载请注明。并且保留文章链接。否则保留追究法律责任的权利。