问题
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