[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."

 

加上一段参考代码。。。

 

代码
@property(nonatomic, retain) UITextField *userName编译时自动生成的代码 

- (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

posted @ 2010-02-24 15:11  AlexLiu  阅读(2636)  评论(4编辑  收藏  举报