weak 和 unsafe_unretained 的区别 [英] Differences between weak and unsafe_unretained

查看:22
本文介绍了weak 和 unsafe_unretained 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个支持 iOS 4 的项目,所以我所有的 IBOutlets 都是 __unsafe_unretained 甚至 IBOutlets 在 nib 中但在控制器主视图之外(同一个 nib 中的单独视图)并且所有工作很棒.

So I had a project that supported iOS 4, so all my IBOutlets were __unsafe_unretained even IBOutlets that were in the nib but outside the controllers main view (Separate View in the same nib) and all worked great.

时间到了,现在客户只想支持 iOS 5,所以我们的团队将所有 __unsafe_unretained IBOutlets 更改为 __weak IBOutlets 但现在不支持的 IBOutlets主视图内部设置为 nil(viewdidload 除外),因此我们以后无法添加它们.

So the time has come and now the client wants to support only iOS 5 so our team changed all the __unsafe_unretained IBOutlets for __weak IBOutlets but now the IBOutlets that are not inside the main view are set to nil (except in viewdidload) so we are unable to add them later.

如果我考虑一下,这是有道理的,因为如果没有视图(主视图)保留那些 IBOutlets,它们应该被释放并归零(我不知道这是否是正确的词),所以解决方案是删除来自那些 IBOutlets 的 __weak

If I think about it, it makes sense because if no view (main view) is retaining those IBOutlets they should be deallocated and zeroed (I don't know if that is the correct word), so the solution is to remove the __weak from those IBOutlets

但是对我来说没有意义的是为什么 unsafe_unretainedweak 之间的不同行为,在我的脑海中 unsafe_unretained 应该被释放,当应用程序尝试访问它们时,它们应该指向一个无效的引用,然后应用程序应该崩溃.

But what doesn't make sense to me is Why the different behavior between unsafe_unretained and weak, in my head the unsafe_unretained ones should be deallocated and when the app tries to access them, they should point to an invalid reference and then the app should crash.

我认为 unsafe__unretained 与 weak 相同,但没有归零.

I thought that unsafe__unretained was the same as weak but without the zeroing.

我错过了什么吗?

谢谢.

推荐答案

我认为 unsafe__unretained 与 weak 相同,但没有归零.

I thought that unsafe__unretained was the same as weak but without the zeroing.

是的,是的.

当 Cocoa 加载 nib 时,它会创建所有自动释放的对象,因此在调用 viewDidLoad 时它们仍然存在.但是,自动释放池的生命周期在控制权返回到运行循环时结束.此时,所有不属于任何东西的对象都将消失,因此任何弱出口都将在此时归零.

When Cocoa loads the nib, it creates all the objects autoreleased, so they are still there when viewDidLoad is invoked. However, the autorelease pool has a lifetime that ends when control returns to the run loop. At this point all the objects that aren't owned by anything will go away so any weak outlets will be zeroed at that point.

对于大多数商店来说,这不是问题,因为 NIB 中的对象通常已经由某物拥有.因此,例如,视图中的按钮归其父视图所有.因此,拥有指向该按钮的强大出口是矫枉过正或更糟的可能导致保留周期.

For most outlets, this is not a problem because objects in the NIB are already generally owned by something anyway. So, for instance, a button in a view is owned by its parent view. Having strong outlets that point to that button are therefore overkill or worse might result in a retain cycle.

顶级对象显然没有父视图来拥有它们,因此它们需要由其他东西拥有,例如控制器或文件的所有者".如果你发现东西消失了,你需要在 File 的所有者中为它创建一个强大的 IBOutlet.

Top level objects obviously don't have a parent view to own them so they need to be owned by something else e.g. a controller or "File's Owner". If you are finding stuff disappears, you need to create a strong IBOutlet for it in File's owner.

有关更多详细信息,请参阅 Apple 的文档.

For more detail, see Apple's docs.

这篇关于weak 和 unsafe_unretained 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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