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

ope*_*rog 3 iphone memory-management ios automatic-ref-counting

可能重复:
使用ARC,生命周期限定符分配和unsafe_unretained

这两者有什么区别?

@property(unsafe_unretained) MyClass *delegate;
@property(assign) MyClass *delegate;
Run Code Online (Sandbox Code Playgroud)

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

注意:我知道weak哪个是归零引用.但它只有iOS> = 5.

Jos*_*erg 9

在属性访问器中,是的,它们是相同的.assign属性将映射到unsafe_unretained这种情况.但是考虑手动编写ivar而不是使用ivar合成.

@interface Foo
{
   Bar *test;
}
@property(assign) Bar *test;
@end
Run Code Online (Sandbox Code Playgroud)

这个代码现在在ARC下是不正确的,而在ARC之前则没有.所有Obj-C对象的默认属性都__strong在前进.向前迈进的正确方法如下.

@interface Foo
{
   __unsafe_unretained Bar *test;
}
@property(unsafe_unretained) Bar *test;
@end
Run Code Online (Sandbox Code Playgroud)

或者用ivar合成 @property(unsafe_unretained) Bar *test

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