在 Swift 中将函数作为对象传递时如何防止保留循环
想象一下你有一个像这样的数据源对象
import UIKit
class MagicDataSource:NSObject,UITableViewDatasource {
deinit {
println("bye mds")
}
//cant use unowned or weak here
var decorator:((cell:CustomCell)->Void)?
func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath)->UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(Identifier, forIndexPath: indexPath) as CustomCell
decorator?(cell)
return cell
}
}
像这样的视图控制器具有(并且想要)对该对象的强引用
import UIKit
class ViewController: UIViewController {
var datasource:MagicDataSource? = MagicDataSource()
deinit {
println("bye ViewCon")
}
override func viewDidLoad() {
super.viewDidLoad()
datasource?.decorator = decorateThatThing
}
func decorateThatThing(cell:CustomCell) {
//neither of these two are valid
//[unowned self] (cell:CustomCell) in
//[weak self] (cell:CustomCell) in
cell.theLabel.text = "woot"
}
}
当您丢弃视图控制器时,数据源将不会被释放,视图控制器也不会被释放,因为它持有对数据源的强引用。decorateThatThing
视图控制器上的函数。
您可以通过在以下位置执行此操作来停止循环并让装饰器释放ViewController
但感觉很乱
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
datasource?.decorator = nil
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
datasource?.decorator = decorateThatThing
}
所以问题是我如何声明变量和/或函数以避免必须手动拆卸数据源,以便当视图控制器被丢弃时关联的数据源也被释放。