[英]Objective-C declared @property attributes (nonatomic, copy, strong, weak)
[英]In Objective-C with ARC, is it true that we usually only need to specify nonatomic as property attributes?
奇怪的是,在Big Nerd Ranch iOS 5書(第73頁)和編程iOS 5書(O'Reilly,第314頁 ) (updadte:甚至Kochan的Objective-C第四版) ,在ARC的背景下,他們比如屬性屬性的默認值是assign
...但Apple的文檔說默認值strong
。
我還嘗試了一個簡單的程序,如果我沒有指定strong
,程序運行正常,如果我指定strong
,它的工作方式相同,而當使用assign
時,編譯器會顯示警告,所以看起來默認是確實很strong
。
因此,如果大多數時間,我們想要
@property (nonatomic, readwrite, strong) NSMutableArray *foo;
然后我們就可以寫了
@property (nonatomic) NSMutableArray *foo;
因為另外兩個( readwrite
和strong
)是默認的?
readwrite和strong,確實是ARC *下的默認值。 在手動引用計數下,assign是(是)默認值。 我更喜歡明確地指定這些,因為它使得@屬性的參數更清楚,而不是依賴於知道默認值的人閱讀代碼。
* strong是默認值,假設你讓編譯器為你合成一個實例變量,或者聲明了一個沒有顯式所有權限定符的實例變量(在這種情況下,ivar默認為__strong
)。 否則,默認屬性所有權類型與ivar聲明中的所有者限定符匹配。 因此,如果您使用__weak
顯式聲明了一個ivar,然后在沒有所有權限定符的情況下為它聲明了@property,則合成屬性將很弱。 這些都記錄在Clang ARC文檔中 。
默認情況下,對象屬性是strong, atomic, readwrite
。 請參閱https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.