简体   繁体   English

Swift UITableView委托和dataSource声明和保留周期

[英]Swift UITableView delegate and dataSource declaration and retain cycles

As far as I understood, to use the delegate pattern in swift I had to declare a property like so: 据我所知,要在swift中使用委托模式,我必须声明一个属性,如下所示:

weak var delegate: TheDelegateProtocol!

And a protocol like so: 和这样的协议:

@class_protocol protocol TheDelegateProtocol {

}

To avoid retain cycle and stick with what we are used to do in objective C. 为了避免保留周期并坚持我们在目标C中的习惯。

Now, if I look at what they have in the UITableView definition, I only see: 现在,如果我看看它们在UITableView定义中的含义,我只会看到:

var dataSource: UITableViewDataSource!
var delegate: UITableViewDelegate!

And: 和:

protocol UITableViewDelegate : NSObjectProtocol, UIScrollViewDelegate {
    [...]
}

I guess that's related to the fact that it is actually only bindings to Objective C, and the objective C definition might take precedence over the swift definition, but I can't find an official explanation in the documentation. 我猜这与它实际上只是绑定到Objective C的事实有关,并且目标C定义可能优先于快速定义,但我在文档中找不到官方解释。

This is for the same reason that in general many Cocoa delegates are not weak. 出于同样的原因,一般来说很多Cocoa代表并不弱。 Large parts of Cocoa are not written using ARC - because they precede it. Cocoa的大部分都不是用ARC编写的 - 因为它们先于它。 They are managing memory manually, as we used to have to do in the good old days. 他们正在手动管理内存,就像我们过去常常要做的那样。 So they don't get the delights of ARC-weak (which is what weak indicates here). 所以他们没有得到ARC弱者的喜悦(这就是weak所指的)。 They use pure, non-memory-managed assignment of delegates (and data sources). 它们使用纯的,非内存管理的委托(和数据源)分配。 They don't retain it, so there's no retain cycle; 他们没有保留它,所以没有保留周期; but since they are not using ARC, they are not crash-safe. 但由于他们没有使用ARC,因此它们不具备防撞保护功能。

Thus it remains your responsibility not to let such a delegate go out of existence during the lifetime of the primary instance, lest it attempt to send a message to a dangling pointer and crash. 因此,在主要实例的生命周期内,不让这样的代理人不再存在,以免它试图向悬空指针发送消息并崩溃,这仍然是你的责任。

You can see this by experimentation (this is Objective-C but you'll readily see the point): 你可以通过实验看到这一点(这是Objective-C,但你很容易看到这一点):

self->_obj = [NSObject new];
nav.delegate = self->_obj // nav is our navigation controller, the root view controller
dispatch_async(dispatch_get_main_queue(), ^{
    // cry havoc, and let slip the dogs of war!
    self->_obj = nil; // releases obj - now what is nav.delegate pointing to??
    NSLog(@"Nav Controller delegate: %@", ((UINavigationController*)self.window.rootViewController).delegate); // if you're lucky it might print something!
    // or more likely it will just crash, or maybe print and *then* crash
});

That kind of crash is exactly what ARC-weak prevents, because it replaces the dangling pointer by nil automatically - and a message to nil, in Objective-C, is harmless. 这种崩溃正是ARC弱点所阻止的,因为它会自动将nilling指针替换为nil - 而在Objective-C中,nil的消息是无害的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM