强/弱/保留/unsafe_unretained/分配 [英] strong / weak / retain / unsafe_unretained / assign

查看:34
本文介绍了强/弱/保留/unsafe_unretained/分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于合成属性的属性:保留/分配

  • retain - 它被保留,旧值被释放并被分配
  • assign - 仅分配

所有权属性:IOS5 = 强/弱IOS4 = 保留/unsafe_unretained

properties for ownership : IOS5 = strong / weak IOS4 = retain / unsafe_unretained

  • 强(iOS4 = 保留)-我是所有者,您无法解除分配在瞄准之前 = 保留

  • strong (iOS4 = retain) - i'am the owner, you cannot dealloc this before aim fine with that = retain

weak (iOS4 = unsafe_unretained) - 和assign一样,没有retain或释放

weak (iOS4 = unsafe_unretained) - the same thing as assign, no retain or release

所以 unsafe_unretained == 赋值?

@property (nonatomic, assign) NSArray * tmp;

等于 ?

@property (nonatomic, unsafe_unretained) NSArray * tmp;

反之亦然?

如果是这样,在 iOS4 中更喜欢哪个,或者如果 (unsafe_unretained) 与assign 完全相同,为什么会出现?

iOS4 中的委托应该是 unsafe_unretained 还是assign?

and a delegate in iOS4 should be unsafe_unretained or assign?

推荐答案

如果是这样,在 iOS4 中更喜欢哪个,或者如果 (unsafe_unretained) 与assign 完全相同,为什么会出现?

if so, which one to prefer when in iOS4, or why there is (unsafe_unretained) if its exactly same as assign?

你应该使用unsafe_unretained.您想向代码的读者展示您实际上想要使用弱,但这是不可能的,因为弱在您要部署的 iOS 版本上不可用.

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.

总有一天你会放弃对 iOS4 的支持.然后你可以只搜索 unsafe_unretained 并将它们全部替换为 weak.这比搜索 assign 并确定您的意思是 assign 还是 weak

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

unsafe_unretained 的使用创建了更具可读性和可理解性的代码,其中开发人员的意图更容易看出.基本上与我们使用 YES 而不是 1 的原因相同.

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.

这篇关于强/弱/保留/unsafe_unretained/分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆