为什么我更喜欢unsafe_unretained限定符而不是赋值给弱引用属性?
可能重复:
使用ARC,生命周期限定符分配和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
所以它真的只是一种不同的写作方式,但它表现出不同的意图。