歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS 5中的strong和weak關鍵字

iOS 5中的strong和weak關鍵字

日期:2017/3/1 10:29:23   编辑:Linux編程

iOS 5 中對屬性的設置新增了strong 和weak關鍵字來修飾屬性(iOS 5 之前不支持ARC)

strong 用來修飾強引用的屬性;

@property (strong) SomeClass * aObject;
對應原來的
@property (retain) SomeClass * aObject; 和 @property (copy) SomeClass * aObject;

weak 用來修飾弱引用的屬性;
@property (weak) SomeClass * aObject;
對應原來的
@property (assign) SomeClass * aObject;

__weak, __strong 用來修飾變量,此外還有 __unsafe_unretained, __autoreleasing 都是用來修飾變量的。
__strong 是缺省的關鍵詞。
__weak 聲明了一個可以自動 nil 化的弱引用。
__unsafe_unretained 聲明一個弱應用,但是不會自動nil化,也就是說,如果所指向的內存區域被釋放了,這個指針就是一個野指針了。
__autoreleasing 用來修飾一個函數的參數,這個參數會在函數返回的時候被自動釋放。

Copyright © Linux教程網 All Rights Reserved