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

落花浮王杯 提交于 2019-12-03 21:37:15

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.

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