Why should I prefer the unsafe_unretained qualifier over assign for weak referencing properties? [duplicate]

我怕爱的太早我们不能终老 提交于 2020-01-01 07:01:18

问题


Possible Duplicate:
using ARC, lifetime qualifier assign and unsafe_unretained

What's the difference between the two?

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

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?

Note: I know there is weak which is a zeroing reference. But it's only iOS >= 5.


回答1:


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

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

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.



来源:https://stackoverflow.com/questions/8806831/why-should-i-prefer-the-unsafe-unretained-qualifier-over-assign-for-weak-referen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!