ios 委托的属性“分配”和“保留”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5176261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
property "assign" and "retain" for delegate
提问by Forrest
For iOS developers, delegates are used almost everywhere.
对于 iOS 开发者来说,委托几乎无处不在。
And seems like that we need to use "assign" instead of retain for a delegate like this
似乎我们需要对这样的委托使用“分配”而不是保留
@property(assign) id delegate;
The reason is to avoid the circular loop issue Why are Objective-C delegates usually given the property assign instead of retain?
原因是为了避免循环循环问题为什么Objective-C 委托通常赋予属性assign 而不是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?
我看到了很多代码,他们仍然使用“保留”。所以这里的问题是,如果我们对委托使用保留,我们还会遇到循环循环问题吗?
Thanks
谢谢
回答by vfn
The documentationsays:
该文件说:
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
保留对象会创建强引用,并且在释放所有强引用之前,无法释放对象。如果两个对象相互保留,则两个对象都不会被释放,因为它们之间的连接无法断开
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.
作为一个例子,让我们考虑一个实现 UITableViewDelegate 协议的 UITableViewController。UITableView 由它的视图控制器保留,尽管 UITableView 不保留它的委托。
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.
正如上面的文档所说, UITableViewController 只有在其所有强引用都被释放时才会完成它的释放。由于以 UItableViewController 作为委托的 UITableView 不会保留它,当 UItableViewController 的所有者对其调用 release 时,保留计数将变为零,并且将调用 dealloc 方法。
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.
现在想象一下 UITableView 保留了它的委托。UITableViewController 的保留计数至少为 +2。一个是它的所有者,另一个是 UITableView。当 UITableViewController 的所有者对其调用 release 时,保留计数将变为 +1,而不是预期的零,因此在保留计数达到零之前不会调用 dealloc 方法。要达到零,UITableViewController 需要释放它的 UITableView,然后释放它的委托(UITableViewController)。因为 UITableViewController 只会在释放时处理它的视图(UITableView),这一刻永远不会发生,因为保留计数不会低于 +1。
(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. :))
(让我们不要考虑内存警告和任何其他可能的情况......我刚刚看到 ViewController/View 不是这个例子的最佳选择,但我已经写了太多了。:))
Does that make sense?
那有意义吗?