• <tfoot id='U7BJj'></tfoot>

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

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

      <legend id='U7BJj'><style id='U7BJj'><dir id='U7BJj'><q id='U7BJj'></q></dir></style></legend>
      <i id='U7BJj'><tr id='U7BJj'><dt id='U7BJj'><q id='U7BJj'><span id='U7BJj'><b id='U7BJj'><form id='U7BJj'><ins id='U7BJj'></ins><ul id='U7BJj'></ul><sub id='U7BJj'></sub></form><legend id='U7BJj'></legend><bdo id='U7BJj'><pre id='U7BJj'><center id='U7BJj'></center></pre></bdo></b><th id='U7BJj'></th></span></q></dt></tr></i><div id='U7BJj'><tfoot id='U7BJj'></tfoot><dl id='U7BJj'><fieldset id='U7BJj'></fieldset></dl></div>
      1. 属性“分配";和“保留"代表

        时间:2023-07-28

        • <bdo id='osmAq'></bdo><ul id='osmAq'></ul>
          <legend id='osmAq'><style id='osmAq'><dir id='osmAq'><q id='osmAq'></q></dir></style></legend>

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

              <i id='osmAq'><tr id='osmAq'><dt id='osmAq'><q id='osmAq'><span id='osmAq'><b id='osmAq'><form id='osmAq'><ins id='osmAq'></ins><ul id='osmAq'></ul><sub id='osmAq'></sub></form><legend id='osmAq'></legend><bdo id='osmAq'><pre id='osmAq'><center id='osmAq'></center></pre></bdo></b><th id='osmAq'></th></span></q></dt></tr></i><div id='osmAq'><tfoot id='osmAq'></tfoot><dl id='osmAq'><fieldset id='osmAq'></fieldset></dl></div>
            1. <tfoot id='osmAq'></tfoot>
                <tbody id='osmAq'></tbody>
                1. 本文介绍了属性“分配";和“保留"代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  对于 iOS 开发人员,委托几乎无处不在.

                  For iOS developers, delegates are used almost everywhere.

                  对于这样的委托,我们似乎需要使用分配"而不是保留

                  And seems like that we need to use "assign" instead of retain for a delegate like this

                  @property(assign) id delegate;
                  

                  原因是为了避免循环循环问题 为什么 Objective-C 委托通常被赋予属性 assign 而不是保留?

                  The reason is to avoid the circular loop issue Why are Objective-C delegates usually given the property assign instead of retain?

                  我看了很多代码,他们仍然使用保留".所以这里的问题是,如果我们对委托使用保留,我们还会遇到循环循环问题吗?

                  I saw a lot of code and they still used "retain". So the question here is will we still get the circular loop issue if we use retain for a delegate?

                  谢谢

                  推荐答案

                  文档 说:

                  保留一个对象会创建一个强引用,并且一个对象在其所有强引用都被释放之前不能被释放.如果两个对象相互保留,则两个对象都不会被释放,因为它们之间的连接不会被破坏

                  Retaining an object creates a strong reference, and an object cannot be deallocated until all of its strong references are released. If two objects retain each other, neither object ever gets deallocated because the connection between them cannot be broken

                  作为一个例子,让我们考虑一个实现 UITableViewDelegate 协议的 UITableViewController.UITableView 由它的视图控制器保留,尽管 UITableView 不保留它的委托.

                  As an example, let's consider a UITableViewController that implements UITableViewDelegate protocol. UITableView is retained by it's view controller, although the UITableView does not retain it's delegate.

                  正如上面文档中所说,UITableViewController 只有在其所有强引用都被释放时才会完成其释放.由于以 UItableViewController 作为委托的 UITableView 不保留它,所以当 UItableViewController 的所有者对其调用 release 时,保留计数将归零并调用 dealloc 方法.

                  As said on the document above, UITableViewController will only complete its deallocation when all its strong references get released. Since the UITableView that has the UItableViewController as a delegate doesn't retain it, when the owner of UItableViewController calls release on it, the retain count will go to zero and the dealloc method will get called.

                  现在假设 UITableView 保留了它的委托.UITableViewController 的保留计数至少为 +2.一个是它的所有者,另一个是 UITableView.当 UITableViewController 的所有者对其调用 release 时,保留计数将变为 +1,而不是预期的为零,因此在保留计数达到零之前不会调用 dealloc 方法.为了达到零,UITableViewController 需要释放它的 UITableView,然后释放它的委托(UITableViewController).因为 UITableViewController 只会在释放它的视图(UITableView)时释放这个时刻永远不会发生,因为保留计数不会低于 +1.

                  Now imagine that UITableView retains its delegate. UITableViewController will have a retain count of at least +2. One with it's owner and another with UITableView. When UITableViewController's owner calls release on it, the retain count will go to +1, and not to zero as it was expected, and so the dealloc method won't get called until the retain count reaches zero. To reach zero, UITableViewController would need to release its UITableView that would then release its delegate (UITableViewController). Because the UITableViewController will only disposes its view (UITableView) when deallocing this moment would never happen because the retain count won't go bellow +1.

                  (让我们不要考虑内存警告和任何其他可能的情况......我刚刚看到 ViewController/View 不是这个示例的最佳选择,但我已经写了太多了.:))

                  (let's not take in consideration memory warnings and any other possible case...I just saw that ViewController/View is not the best option for this example, but I've written too much already. :))

                  这有意义吗?

                  这篇关于属性“分配";和“保留"代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从应用程序委托获取当前视图控制器(模式是可能的) 下一篇:iPhone 中的代理是什么?

                  相关文章

                2. <tfoot id='FogK7'></tfoot>
                    <bdo id='FogK7'></bdo><ul id='FogK7'></ul>

                3. <legend id='FogK7'><style id='FogK7'><dir id='FogK7'><q id='FogK7'></q></dir></style></legend>

                    1. <small id='FogK7'></small><noframes id='FogK7'>

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