Possible Duplicate:
using ARC, lifetime qualifier assign and unsafe_unretained
两者有什么区别?
@property(unsafe_unretained) MyClass *delegate;
@property(assign) MyClass *delegate;
两者都是非归零弱引用,对吧?那么有什么理由让我写更长更难读的 unsafe_unretained
而不是 assign
?
注意:我知道 weak
是一个归零引用。但它只是 iOS >= 5。
在属性访问器中,是的,它们是相同的。在这种情况下,assign
属性将映射到 unsafe_unretained
。但请考虑手动编写 ivar 而不是使用 ivar 合成。
@interface Foo
{
Bar *test;
}
@property(assign) Bar *test;
@end
此代码现在在 ARC 下是不正确的,而在 ARC 之前它不是。所有 Obj-C 对象的默认属性是 __strong
向前发展。向前推进的正确方法如下。
@interface Foo
{
__unsafe_unretained Bar *test;
}
@property(unsafe_unretained) Bar *test;
@end
或者使用ivar合成只是@property(unsafe_unretained) Bar *test
所以实际上它只是写同一件事的不同方式,但它显示了不同的意图。