#13: Strong References
Question
// Swift Quiz
— objc.io (@objcio) May 15, 2019
class A {
var b: B
init(b: B) {
self.b = b
}
deinit {
print("A")
}
}
class B {
weak var a: A?
deinit {
print("B")
}
}
var b: B? = B()
var a: A? = A(b: b!)
b?.a = a
b = nil
// What's the output?
Solution
Solution: None of them
— objc.io (@objcio) May 16, 2019
Because a is still in scope and not nil, the instance of A is strongly referenced. In turn, it holds a strong reference to the instance of B.
Only once we set a to nil are both objects deinited.
https://t.co/s7500CWmAg