關於IOS中變量聲明方式@interface和@property的區別

最近在在示例的時候總是對於@interface和@property聲明的困惑,起初的時候對這個沒有怎麼在意,只是一味的在@interface中放了一個,在@property中再放一個,然後再在類中@synchronize, 後面又看到有的示例中在聲明變量的時候有的只用@interface 有的只用@property, 有的是@interface和@property混和交叉

在網了找了下資料,發現這篇講得最好,個人理解,主要還是訪問方式和在用於內存管理時個的區別.以下爲轉載.

用了一段oc;會發現有2種定義變量的方式
1.在  @interface :NSObject{} 的括號中,當然NSObject 是指一個父類,可以是其他的。
形式如下:

@interface GCTurnBasedMatchHelper : NSObject {
  BOOLgameCenterAvailable;
  BOOLuserAuthenticated;
  }

2.另外一種是直接在 @interface : NSObject{}括號之後,用 @property 去定義一個變量。
[objc]@property (assign, readonly) BOOL gameCenterAvailable;[/objc]
你會發現,有人會再@interface中定義了變量後,又在 @property中重複定義相同的變量,而且很常見。
結果可能是這樣:

@interface GCTurnBasedMatchHelper : NSObject {
BOOLgameCenterAvailable;
BOOLuserAuthenticated;
 }
@property (assign, readonly) BOOLgameCenterAvailable;

而且你可以單獨在@interface中定義變量,而不用@property定義;也可以只用@property去定義,而不在 @interface中定義,當然用了@property去定義,一般要在.m文件中用@synthsize去合成相應的setter,getter方 法。否則會得到一個警告。當然@synthsize是可選的,但是是Apple推薦的,不用會有什麼後果,我沒試過,有興趣的童鞋可以試一下。
那這兩種方式有什麼區別呢。
1. 只在@interface中定義變量的話,你所定義的變量只能在當前的類中訪問,在其他類中是訪問不了的;而用@property聲明的變量可以在外部訪問。
2.用了@property去聲明的變量,可以使用“self.變量名”的方式去讀寫變量。而用@interface的方式就不可以。
3.  這裏給出一個鏈接:http://stackoverflow.com/questions/9702258/difference-between-properties-and-variables-in-ios-header-file    裏面講到:  我英語菜,簡單翻一下:
Defining the variables in the brackets simply declares them instance variables.
在括號中定義一個變量只是簡單的聲明瞭一個實例變量(實例變量應該指的成員變量)。  博主注:老外對variable 和instance variable是有不同理解的。所以下文中 用了一個模糊的詞 ivar。
Declaring (and synthesizing) a property generates getters and setters for the instance variable, according to the criteria within the parenthesis. This is particularly important in Objective-C because it is often by way of getters and setters that memory is managed (e.g., when a value is assigned to an ivar, it is by way of the setter that the object assigned is retained and ultimately released). Beyond a memory management strategy, the practice also promotes encapsulation and reduces the amount of trivial code that would otherwise be required.
聲明(和 @synthsize)一個屬性會爲成員變量生成 getter 和setter方法,根據括號內的標準,在oc中經常用setter和getter 做內存管理,這是很重要的。(例如: 當一個值被賦給這個變量,對象是通過setter函數去分配,修改計數器,並最後釋放的)。更高一個層次來說,這種做法也促進了封裝,減少了一些不必要的 代碼。
It is very common to declare an ivar in brackets and then an associated property (as in your example), but that isn’t strictly necessary. Defining the property and synthesizing is all that’s required, because synthesizing the property implicitly also creates an ivar.
在@interface括號中定義一個變量並用@property 重複定義一次是很普遍的,實際上不是必要的。用@property和@synthszie就夠了,因爲在用@synthsize合成這個屬性的讀寫方法時就會創建一個變量。
The approach currently suggested by Apple (in templates) is:
目前蘋果(在模板中)建議的方法是這樣的:
-Define property in header file, e.g.:
先在頭文件中定義一個屬性

@property intgameCenter;

Then synthesize & declare ivar in implementation:
然後在實現文件中  synthsize和declare成這樣:

@synthesize gameCenter = _gameCenter;

The last line synthesizes the gameCenter property and asserts that whatever value is assigned to the property will be stored in the __gameCenter ivar. Again, this isn’t necessary, but by defining the ivar next to the synthesizer, you are reducing the locations where you have to type the name of the ivar while still explicitly naming it.
最後一行synthsize  gameCenter 屬性並說明了不管什麼值被分配給這個屬性,都會存儲到_gameCenter這個變量中。 再次說明,這不是必要的,但是,這樣寫了之後,你能減少輸入已經明確命名的變量名。
最後一句的意思you are reducing the locations where you have to type the name of the ivar while still explicitly naming it .不好翻。
據千鋒的第2節語法課課程的講解,這樣寫之後可以使得 @synthsize 時內部getter方法會展成

-(int)gameCenter
{
return  _gameCenter;
}

而直接寫

@synthsize  gameCenter;

setter函數會在內部展開成

-(int)gameCenter
{
   return  gameCenter;
}



注意到:函數名和變量名是一樣的。在斯坦福的課程中,白鬍子教授也模糊的說道這樣的同名有可能帶來bug,具體什麼bug他沒說,我也沒見過, 所以還是養成這樣寫的習慣爲好。其他語言的getter函數  一般會在變量前加 get;但oc沒有,可能是爲了與其他語言做區分,算是oc的特色,結果卻帶來這麼個麻煩。

xcode在早期@systhesize沒有自動合成屬性器之前,需要手寫

getter與setter方法,下劃線從風格上表明這是類的內部變量,要是需要直接使用變量則需要使用get或者set的方式。

在XCode目前有了自動合成屬性器後,編譯器會自動幫我們生成一個以下劃線開頭的的實例變量,所以我們不必去同時聲明屬性與變量。 我們可以直接用@property的方式來聲明一個成員屬性,在.m文件中使不使用@systhesize都無所謂,xcode會自動幫你生成getter與setter.這是是蘋果開發模板所推薦的,也可以在.m文件中不加@systhesize看個人喜好吧。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章