Objective-C Property 和 Synthesize

Objective-C中的@property、@synthesize及點語法都是與兩個函數有關的,一是setter函數,另外一個是getter函數多線程

之前咱們是這樣來定義setter與getter函數的函數

@interface Dog:NSObject  
{  
    int age;  
}  
  
-(void)setAge:(int)newAge;  
-(void)age;   
@end  
  
@implementation Dog  
  
-(void)setAge:(int)newAge  
{  
    age = newAge;  
}  
-(void)age  
{  
    return age;  
}  
@end


而現在咱們有了更好的方法:性能

@property是讓編譯器自動產生setter與getter的函數聲明atom

@sythesize就是讓編譯器自動實現setter與getter函數spa

現現在咱們這樣寫就能夠了線程

@interface Dog:NSObject  
{  
    int age;  
}  
  
@property int age;  
  
@end  
  
@implementation Dog  
  
@synthesize age;  
@end

當@properpty後是非基本數據類型時,能夠加參數code

有關點語法:ci

dog.age = 10;
dogAge = [dog ahe];

編譯器會把dog.age = 10;展開成[dog setAge:10];資源

會把dogAge = dog.age;展開成dogAge = [dog age];get

點語法在等號的左邊,它就是一個setter函數,點語法在等號的右邊就是一個getter函數。

參考一下前輩們的總結:

聲明property的語法爲:@property (參數1,參數2) 類型 名字;

@property(nonatomic,retain) UIWindow *window;    

其中參數主要分爲三類:

讀寫屬性: (readwrite/readonly)

setter語意:(assign/retain/copy)

原子性: (atomicity/nonatomic)

各參數意義以下:

readwrite: 產生setter\getter方法

readonly: 只產生簡單的getter,沒有setter。

assign: 默認類型,setter方法直接賦值,而不進行retain操做

retain: setter方法對參數進行release舊值,再retain新值。

copy: setter方法進行Copy操做,與retain同樣

nonatomic: 禁止多線程,變量保護,提升性能


參數類型

參數中比較複雜的是retain和copy,具體分析以下:

getter 分析

@property(nonatomic,retain)test* thetest;    
@property(nonatomic ,copy)test* thetest;

等效代碼:

-(void)thetest    
{    
  return thetest;    
}
@property(retain)test* thetest;    
@property(copy)test* thetest;

等效代碼:

-(void)thetest    
{    
    [thetest retain];    
    return [thetest autorelease];    
}


setter分析

@property(nonatomic,retain)test* thetest;    
@property(retain)test* thetest;

等效於:

-(void)setThetest:(test *)newThetest {    
    if (thetest!= newThetest) {    
        [thetestrelease];    
        thetest= [newThetest retain];    
    }    
}
@property(nonatomic,copy)test* thetest;    
@property(copy)test* thetest;

 等效於:

-(void)setThetest:(test *)newThetest {    
    if (thetest!= newThetest) {    
        [thetest release];    
        thetest= [newThetest copy]; 
       }
}


nonatomic

若是使用多線程,有時會出現兩個線程互相等待對方致使鎖死的狀況(具體能夠搜下線程方面的注意事項去了解)。在沒有(nonatomic)的狀況下,即默認(atomic),會防止這種線程互斥出現,可是會消耗必定的資源。因此若是不是多線程的程序,打上(nonatomic)便可

retain

代碼說明

若是隻是@property NSString*str; 則經過@synthesize自動生成的setter代碼爲:

-(void)setStr:(NSString*)value{    
    str=value;    
}


若是是@property(retain)NSString*str; 則自動的setter內容爲:

-(void)setStr:(NSString*)v{    
    if(v!=str){    
        [str release];    
        str=[v retain];    
    }    
}


在這個看到的一個比較糾結的地方

@synthesize window=_window; 意思是說,window 屬性爲 _window 實例變量合成訪問器方法。

也就是說,window屬性生成存取方法是setWindow,這個setWindow方法就是_window變量的存取方法,它操做的就是_window這個變量。

下面是一個常見的例子

@interface MyClass:NSObject{
  MyObjecct *_myObject;
}
@property(nonamtic, retain) MyObjecct *myObject;
@end
@implementatin MyClass
@synthesize myObject=_myObject;

這個類中聲明瞭一個變量_myObject,又聲明瞭一個屬性叫myObject,而後用@synthesize生成了屬性myObject的存取方法,這個存取方法的名字應該是:setmyObject和getmyObject。@synthesize myObject=_myObject的含義就是屬性myObject的存取方法是作用於_myObject這個變量的。

這種用法在Apple的Sample Code中很常見

相關文章
相關標籤/搜索