[英]Assigning UITableView delegate and dataSource to super in Swift
[英]Swift UITableView delegate and dataSource declaration and retain cycles
据我所知,要在swift中使用委托模式,我必须声明一个属性,如下所示:
weak var delegate: TheDelegateProtocol!
和这样的协议:
@class_protocol protocol TheDelegateProtocol {
}
为了避免保留周期并坚持我们在目标C中的习惯。
现在,如果我看看它们在UITableView定义中的含义,我只会看到:
var dataSource: UITableViewDataSource!
var delegate: UITableViewDelegate!
和:
protocol UITableViewDelegate : NSObjectProtocol, UIScrollViewDelegate {
[...]
}
我猜这与它实际上只是绑定到Objective C的事实有关,并且目标C定义可能优先于快速定义,但我在文档中找不到官方解释。
出于同样的原因,一般来说很多Cocoa代表并不弱。 Cocoa的大部分都不是用ARC编写的 - 因为它们先于它。 他们正在手动管理内存,就像我们过去常常要做的那样。 所以他们没有得到ARC弱者的喜悦(这就是weak
所指的)。 它们使用纯的,非内存管理的委托(和数据源)分配。 他们没有保留它,所以没有保留周期; 但由于他们没有使用ARC,因此它们不具备防撞保护功能。
因此,在主要实例的生命周期内,不让这样的代理人不再存在,以免它试图向悬空指针发送消息并崩溃,这仍然是你的责任。
你可以通过实验看到这一点(这是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
});
这种崩溃正是ARC弱点所阻止的,因为它会自动将nilling指针替换为nil - 而在Objective-C中,nil的消息是无害的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.