我很难弄清楚如何确保何时在封闭体中使用[弱自我]/[无主自我].在下面显示的两个场景中,根据我的说法,它取决于B类是否拥有传递的闭包.
现在如果隐藏了B类的实现,我不确定如何决定使用[弱自我]/[无主自我].
有人可以帮我理解你将如何决定?
/******** Scenario 1 **********/
class A {
var b:B?
let p = "Some Property of A"
init() {
print("Init of A")
self.b = B(closure: { (number) -> Void in
print(self.p) // capturing self but still no need to write [weak/unowned self]
print(number)
})
}
deinit {
print("Deinit of A")
}
}
// Suppose this is a library class whose implementation is hidden
class B {
init(closure:(Int->Void)) {
print("Init of B")
// ... do some work here …Run Code Online (Sandbox Code Playgroud)