typealias Closer = ((_ string1 : String, _ string2 : String) -> Void)
var block:Closer?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let vc = ViewController()
weak var weakSelf = vc
self.block = { (string1 : String, string2 : String) -> Void in
print(weakSelf)
let strong = weakSelf
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5, execute: {
print(strong)
})
}
self.block?("", "")
}
打印结果:
Optional(<Demo.ViewController: 0x7fd93a40ace0>)
Optional(<Demo.ViewController: 0x7fd93a40ace0>)
如果block中不进行强引用,而直接强行拆包,会引起奔溃。这在OC中更能体现。
打印结果:
Optional(<Demo.ViewController: 0x7fd93a40ace0>)
nil
网友评论