原文地址:http://blog.sina.com.cn/s/blog_9075354e0102uwwt.html
一、類Class中的屬性property
在ios第一版中,我們為輸出口同時聲明了屬性和底層實例變量,那時,屬性是oc語言的一個新的機制,并且要求你必須聲明與之對應的實例變量,例如:
@interface MyViewController :UIViewController
{
UIButton *myButton;
}
@property (nonatomic, retain) UIButton *myButton;
@end
最近,蘋果將默認編譯器從GCC轉換為LLVM(low level virtual machine),從此不再需要為屬性聲明實例變量了。如果LLVM發現一個沒有匹配實例變量的屬性,它將自動創建一個以下劃線開頭的實例變量。因此,在這個版本中,我們不再為輸出口聲明實例變量。
例如:MyViewController.h文件
@interface MyViewController :UIViewController
@property (nonatomic, retain) UIButton *myButton;
@end
在MyViewController.m文件中,編譯器也會自動的生成一個實例變量_myButton。那么在.m文件中可以直接的使用_myButton實例變量,也可以通過屬性self.myButton.都是一樣的。
注意這里的self.myButton其實是調用的myButton屬性的getter/setter方法。這與C++中點的使用是有區別的,C++中的點可以直接訪問成員變量(也就是實例變量)。
例如在oc中有如下代碼
.h文件
@interface MyViewController :UIViewController
{
NSString *name;
}
@end
.m文件中,self.name 這樣的表達式是錯誤的。xcode會提示你使用->,改成self->name就可以了。因為oc中點表達式是表示調用方法,而上面的代碼中沒有name這個方法。
oc語法關于點表達式的說明:"點表達式(.)看起來與C語言中的結構體訪問以及java語言匯總的對象訪問有點類似,其實這是oc的設計人員有意為之。如果點表達式出現在等號 = 左邊,該屬性名稱的setter方法將被調用。如果點表達式出現在右邊,該屬性名稱的getter方法將被調用。"
所以在oc中點表達式其實就是調用對象的setter和getter方法的一種快捷方式, 例如:dealie.blah = greeble 完全等價于 [dealie.blah setBlah:greeble];
以前的用法,聲明屬性跟與之對應的實例變量:
@interface MyViewController :UIViewControlle
{
UIButton *myButton;
}
@property (nonatomic, retain) UIButton *myButton;
@end
這種方法基本上使用最多,現在大部分也是在使用,因為很多開源的代碼都是這種方式。但是ios5更新之后,蘋果是建議以以下的方式來使用:
@interface MyViewController :UIViewController
@property (nonatomic, retain) UIButton *myButton;
@end
因為編譯器會自動為你生成以下劃線開頭的實例變量_myButton,不需要自己手動再去寫實例變量。而且也不需要在.m文件中寫@synthesize myButton;也會自動為你生成setter,getter方法。@synthesize的作用就是讓編譯器為你自動生成setter與getter方法。
@synthesize 還有一個作用,可以指定與屬性對應的實例變量,例如@synthesize myButton = xxx;那么self.myButton其實是操作的實例變量xxx,而不是_myButton了。
在實際的項目中,我們一般這么寫.m文件
@synthesize myButton;
這樣寫了之后,那么編譯器會自動生成myButton的實例變量,以及相應的getter和setter方法。注意:_myButton這個實例變量是不存在的,因為自動生成的實例變量為myButton而不是_myButton,所以現在@synthesize的作用就相當于指定實例變量;
如果.m文件中寫了@synthesize myButton;那么生成的實例變量就是myButton;如果沒寫@synthesize myButton;那么生成的實例變量就是_myButton。所以跟以前的用法還是有點細微的區別。
二、類別中的屬性property
類與類別中添加的屬性要區分開來,因為類別中只能添加方法,不能添加實例變量。經常會在ios的代碼中看到在類別中添加屬性,這種情況下,是不會自動生成實例變量的。比如在:UINavigationController.h文件中會對UIViewController類進行擴展
@interface UIViewController (UINavigationControllerItem)
@property(nonatomic,readonly,retain) UINavigationItem *navigationItem;
@property(nonatomic) BOOL hidesBottomBarWhenPushed;
@property(nonatomic,readonly,retain) UINavigationController *navigationController;
@end
這里添加的屬性,不會自動生成實例變量,這里添加的屬性其實是添加的getter與setter方法。
注意一點,匿名類別(匿名擴展)是可以添加實例變量的,非匿名類別是不能添加實例變量的,只能添加方法,或者屬性(其實也是方法)。