为什么我更喜欢unsafe_unretained限定符而不是赋值给弱引用属性?

时间:2012-01-10 16:35:51

标签: iphone ios memory-management automatic-ref-counting

  

可能重复:
  using ARC, lifetime qualifier assign and unsafe_unretained

两者之间有什么区别?

@property(unsafe_unretained) MyClass *delegate;
@property(assign) MyClass *delegate;

两者都是非归零弱引用,对吧?那么为什么我应该写更长更难以阅读unsafe_unretained而不是assign

注意:我知道有weak是归零参考。但它只有iOS> = 5。

1 个答案:

答案 0 :(得分:9)

在属性访问器中,是的,它们是相同的。对于这种情况,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

所以它真的只是一种不同的写作方式,但它表现出不同的意图。

相关问题