Objective-C中的@property、@synthesize及點文法

來源:互聯網
上載者:User

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的函式宣告

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

現如今我們這樣寫就可以了

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

當@properpty後是非基礎資料型別 (Elementary Data Type)時,可以加參數

有關點文法:

dog.age = 10;

dogAge = [dog ahe];

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

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

點文法在等號的左邊,它就是一個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 分析

1、

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

等效代碼:

-(void)thetest  {    return thetest;  }  

2、

@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];      }  }  

 2、

@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中很常見

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.