闭包递归和保留循环

Art*_*tov 1 ios swift

我的关闭保留了它自己。它会导致捕获内部的所有其他对象。我可以使用弱引用传递这样的对象,但它并不能解决保留循环的问题。在没有保留周期的情况下使用闭包进行递归的正确方法是什么?

class Foo {
  var s = "Bar"
  deinit {
    print("deinit") // Won't be executed!
  }
}

class TestVC: UIViewController {

  override func viewDidLoad() {
    super.viewDidLoad()

    let foo = Foo() // Weak works, but not the right solution.
    var closure: () -> Void = { return }
    closure = {
      print(foo.s)
      if true {
        return
      } else {
        closure()
      }
    }
  }

}
Run Code Online (Sandbox Code Playgroud)

rob*_*off 5

你有一个不寻常的设置,你的闭包保留了它自己。请注意,Swift 不允许您创建对闭包的弱引用。

要中断保留循环,closure{ }在递归的基本情况下设置为。这是一个测试 macOS 命令行程序:

func test() {
    var closure: ((Int) -> ()) = { _ in }
    closure = { i in
        if i < 10 {
            closure(i + 1)
        } else {
            // Comment out this line for unbounded memory consumption.
            closure = { _ in }
        }
    }
    closure(0)
}

while true {
    test()
}
Run Code Online (Sandbox Code Playgroud)

如果你运行它,它的内存消耗是平的。

如果您在基本情况下注释掉 resets 的那一行closure,它的内存消耗会无限制地增长。