[英]What is the difference between synthesize propertyname = _propertyname
[英]What's the difference between @property and @synthesize?
就像我理解的那樣,@synthesize實際上是生成Getters和Setter。 但是什么是@property呢? 它只是為那個很酷的@synthesize魔術功能設置參數嗎?
@property
將名稱聲明為屬性。 這意味着,它可以通過點語法( object.value
)訪問。
@synthetize
可以看作是一個宏,它創建了getter和setter方法。 知道您可以覆蓋這些方法很有用,即使您鍵入了@synthetize
也是如此。
你在頭文件中寫@property
@property float value;
相當於:
- (float)value;
- (void)setValue:(float)newValue;
它獲取OTHER類的信息,您的類具有此方法
@synthesize phisicaly在類實現中創建這些方法
@property
聲明getter和/或setter
@synthesize
實現它們。
有一個常見的誤解是,為了實現使用@property指令創建的setter和getter,需要@synthesize指令,但事實並非如此。 使用不帶@synthesize的@property指令仍會生成setter / getters,並允許您使用點表示法。 但是,省略@synthesize指令將導致編譯器生成具有前導下划線字符的相應實例變量,例如屬性myVar將具有_myVar的實例變量。
使用前導下划線是一種常見的約定,允許您區分屬性和實例變量。 屬性和實例變量共享同一名稱也很常見,這是@synthesize默認執行的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.