弱与不安全之间的差异 [英] Differences between weak and unsafe_unretained

查看:92
本文介绍了弱与不安全之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个支持iOS 4的项目,所以我的所有IBOutlet都是 __ unsafe_unretained 甚至是在nib中但在控制器主视图之外的IBOutlets(在单独的视图中同样的笔尖,一切都很好。

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 __弱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

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_unretained ,in我的头应该取消分配 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.

我认为不安全的情况与弱势相同但没有归零。

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

我在这里遗漏了什么吗?

Am I missing something here?

谢谢。

推荐答案


我认为不安全的_unretained是相同的虽然很弱但没有归零。

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.

这篇关于弱与不安全之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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