防止 Swift 函数指针中的保留循环

prevent retain cycle in Swift function pointers

在 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
}

所以问题是我如何声明 vars and/or 函数以避免必须手动拆除数据源,以便在丢弃视图控制器时也释放关联的数据源。

而不是

datasource.decorator = decorateThatThing

您可以使用

datasource.decorator = { [unowned self] cell in
    self.decorateThatThing(cell)
}