2013-05-21 29 views
2

將聲明中的@interface{}聲明中的變量放入頭文件並在聲明後放入@property之間有什麼區別?接口聲明中@property和變量之間的區別

例如,

@interface GameCenterManager : NSObject 
{ 
GKInvite* pendingInvite; 
} 
@end 

而非

@interface GameCenterManager : NSObject 
@property (weak, nonatomic) GKInvite* pendingInvite 
@end 
+0

這個問題類似的位置:http://stackoverflow.com/questions/4172810/what-is-the-difference-between-ivars-and- properties-in-objective-c –

回答

3

聲明一個屬性爲實例變量生成getter和setter,根據括號內的標準。

定義括號中的變量只是聲明它們是實例變量。

以下是一些鏈接,提供了更多這些信息。

http://www.cocoawithlove.com/2010/03/dynamic-ivars-solving-fragile-base.html

Is there a difference between an "instance variable" and a "property" in Objective-c?

http://iphonedevelopment.blogspot.in/2008/12/outlets-property-vs-instance-variable.html

+0

不確定,但要生成getter和setter,您必須綜合否? – Vertig0

+1

@PatricioIgnacioFariaValdivi不再適合了。只需'@ property'就足夠了,它會自動創建一個帶有下劃線前綴的iVar。你可以在接口中用'@property(nonatomic)NSString * foo'來測試這個,不用合成你就可以在你的實現中獲得_foo。 – iNoob

+1

@PatricioIgnacioFariaValdivi您無需從ios6進行合成,因爲它是由系統自動合成的。在那裏以前的版本需要你手動合成, –

相關問題