<bdo id='A8cl8'></bdo><ul id='A8cl8'></ul>

  1. <i id='A8cl8'><tr id='A8cl8'><dt id='A8cl8'><q id='A8cl8'><span id='A8cl8'><b id='A8cl8'><form id='A8cl8'><ins id='A8cl8'></ins><ul id='A8cl8'></ul><sub id='A8cl8'></sub></form><legend id='A8cl8'></legend><bdo id='A8cl8'><pre id='A8cl8'><center id='A8cl8'></center></pre></bdo></b><th id='A8cl8'></th></span></q></dt></tr></i><div id='A8cl8'><tfoot id='A8cl8'></tfoot><dl id='A8cl8'><fieldset id='A8cl8'></fieldset></dl></div>
  2. <small id='A8cl8'></small><noframes id='A8cl8'>

    1. <tfoot id='A8cl8'></tfoot>

    2. <legend id='A8cl8'><style id='A8cl8'><dir id='A8cl8'><q id='A8cl8'></q></dir></style></legend>

    3. weak 和 unsafe_unretained 的区别

      时间:2023-06-01

      1. <tfoot id='ycjMk'></tfoot>
        • <bdo id='ycjMk'></bdo><ul id='ycjMk'></ul>
            <tbody id='ycjMk'></tbody>
          <legend id='ycjMk'><style id='ycjMk'><dir id='ycjMk'><q id='ycjMk'></q></dir></style></legend>
        • <i id='ycjMk'><tr id='ycjMk'><dt id='ycjMk'><q id='ycjMk'><span id='ycjMk'><b id='ycjMk'><form id='ycjMk'><ins id='ycjMk'></ins><ul id='ycjMk'></ul><sub id='ycjMk'></sub></form><legend id='ycjMk'></legend><bdo id='ycjMk'><pre id='ycjMk'><center id='ycjMk'></center></pre></bdo></b><th id='ycjMk'></th></span></q></dt></tr></i><div id='ycjMk'><tfoot id='ycjMk'></tfoot><dl id='ycjMk'><fieldset id='ycjMk'></fieldset></dl></div>

              <small id='ycjMk'></small><noframes id='ycjMk'>

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

                问题描述

                限时送ChatGPT账号..

                所以我有一个支持 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 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:使用 UIPopoverBackgroundView 类 下一篇:Objective-C ARC 错误:-fobjc-arc 不支持脆弱的 abi

                相关文章

                最新文章

                  <bdo id='dUDAH'></bdo><ul id='dUDAH'></ul>
              • <small id='dUDAH'></small><noframes id='dUDAH'>

                <i id='dUDAH'><tr id='dUDAH'><dt id='dUDAH'><q id='dUDAH'><span id='dUDAH'><b id='dUDAH'><form id='dUDAH'><ins id='dUDAH'></ins><ul id='dUDAH'></ul><sub id='dUDAH'></sub></form><legend id='dUDAH'></legend><bdo id='dUDAH'><pre id='dUDAH'><center id='dUDAH'></center></pre></bdo></b><th id='dUDAH'></th></span></q></dt></tr></i><div id='dUDAH'><tfoot id='dUDAH'></tfoot><dl id='dUDAH'><fieldset id='dUDAH'></fieldset></dl></div>

                  <tfoot id='dUDAH'></tfoot>
                1. <legend id='dUDAH'><style id='dUDAH'><dir id='dUDAH'><q id='dUDAH'></q></dir></style></legend>