Why should I prefer the unsafe_unretained qualifie

2019-03-31 17:38发布

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条回答
看我几分像从前
2楼-- · 2019-03-31 17:55

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.

查看更多
登录 后发表回答