问题描述
两者有什么区别?
@property(unsafe_unretained) MyClass *delegate;
@property(assign) MyClass *delegate;
两者都是非归零弱引用,对吧?那么有什么理由我应该写更长更难阅读 unsafe_unretained
而不是 assign
?
Both are non-zeroing weak references, right? So is there any reason why I should write the longer and harder to read unsafe_unretained
instead of assign
?
注意:我知道弱
这是一个归零参考。但它只有iOS> = 5.
Note: I know there is weak
which is a zeroing reference. But it's only iOS >= 5.
推荐答案
在属性访问器中,是的,它们是相同的。对于这种情况, assign
属性将映射到 unsafe_unretained
。但是考虑手动编写ivar而不是使用ivar合成。
In a property accessor, yes those are the same. assign
properties will map to unsafe_unretained
for this case. But consider writing the ivar manually rather than using ivar synthesis.
@interface Foo
{
Bar *test;
}
@property(assign) Bar *test;
@end
这个代码现在在ARC下是不正确的,而在ARC之前它是'吨。所有Obj-C对象的默认属性是 __ strong
向前移动。推进这项工作的正确方法如下:
This code is now incorrect under ARC, whereas prior to ARC it wasn't. The default attribute for all Obj-C objects is __strong
moving forward. The proper way to do this moving forward is as follows.
@interface Foo
{
__unsafe_unretained Bar *test;
}
@property(unsafe_unretained) Bar *test;
@end
或使用ivar合成只需 @property(unsafe_unretained )Bar * test
or with ivar synthesis just @property(unsafe_unretained) Bar *test
所以它真的只是一种不同的写作方式,但它表现出不同的意图。
So really its just a different way of writing the same thing, but it shows a different intent.
这篇关于为什么我更喜欢unsafe_unretained限定符而不是赋值给弱引用属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!