强/弱/保留/不安全_保留/分配 [英] strong / weak / retain / unsafe_unretained / assign
问题描述
合成属性的属性:
保留/分配
所有权属性:
IOS5 = strong / weak
IOS4 = retain / unsafe_unretained
-
strong(iOS4 = retain) - 我的所有者,您不能dealloc这个
之前目标精确= = - (iOS4 = unsafe_unretained) - 与分配相同,不保留
或发布
那么unsafe_unretained == assign?
@property(nonatomic,assign)NSArray * tmp;
等于?
@property(nonatomic,unsafe_unretained)NSArray * tmp;
反之亦然?
如果是这样,在iOS4中更喜欢哪一个,或者为什么还有(unsafe_unretained)如果与完全一样分配?
并且iOS4中的委托应该是unsafe_unretained或assign?
更喜欢在iOS4中,或为什么有(unsafe_unretained)如果它完全一样分配?
你应该使用 unsafe_unretained
。你想显示你的代码的读者你实际想使用弱,但这是不可能的,因为weak是不可用的iOS版本你想部署。
有一天,您将放弃对iOS4的支持。然后你可以搜索 unsafe_unretained
,并用 weak
替换所有这些。这将比搜索 assign
更容易,并确定您是否实际上意味着分配
或弱
使用 unsafe_unretained
创建更易读和可理解的代码,开发人员更容易看到。
基本上同样的原因,我们使用 YES
而不是 1
。
properties for synthesizing the property : retain / assign
- retain - it is retained, old value is released and it is assigned
- assign - it is only assigned
properties for ownership : IOS5 = strong / weak IOS4 = retain / unsafe_unretained
strong (iOS4 = retain) - i'am the owner, you cannot dealloc this before aim fine with that = retain
weak (iOS4 = unsafe_unretained) - the same thing as assign, no retain or release
so unsafe_unretained == assign?
@property (nonatomic, assign) NSArray * tmp;
is equal to ?
@property (nonatomic, unsafe_unretained) NSArray * tmp;
and vice versa ?
if so, which one to prefer when in iOS4, or why there is (unsafe_unretained) if its exactly same as assign?
and a delegate in iOS4 should be unsafe_unretained or assign?
if so, which one to prefer when in iOS4, or why there is (unsafe_unretained) if its exactly same as assign?
you should use unsafe_unretained
. You want to show the reader of your code that you actually wanted to use weak but that this was not possible because weak is not available on the iOS version you want to deploy.
One day you will drop the support for iOS4. And then you could just search for unsafe_unretained
and replace all of them with weak
. This will be much easier than searching for assign
and figuring out if you actually meant assign
or weak
The use of unsafe_unretained
creates more readable and understandable code where the intentions of the developer are easier to see.
Basically the same reason we use YES
instead of 1
.
这篇关于强/弱/保留/不安全_保留/分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!