Know when a weak var becomes nil in Swift? Know when a weak var becomes nil in Swift? ios ios

Know when a weak var becomes nil in Swift?


If your button is holding a reference to another view, it should either be an owner of that view (i.e., it should hold a strong reference) or it should not care when that view goes away (i.e., its weak reference to it becomes nil.) There is no notification when weak references become nil, and that is by design.

In particular, Swift property observers are not called when weak references become nil, as the following code demonstrates:

class A : CustomStringConvertible {    var s: String?    init(s: String) {        self.s = s;        print("\(self) init")    }    deinit {        print("\(self) deinit")    }    var description: String {        get { return "[A s:\(s ?? "nil")]" }    }}class B : CustomStringConvertible {    weak var a:A? {        willSet {            print("\(self) willSet a")        }        didSet {            print("\(self) didSet a")        }    }    init(a: A?) {        self.a = a        print("\(self) init")    }    deinit {        print("\(self) deinit")    }    var description: String {        get { return "[B a:\(a == nil ? "nil" : String(describing: a!))]" }    }}func work() {    var a: A? = A(s: "Hello")    var b = B(a: a)    print("\(b)")    a = nil    print("\(b)")    b.a = A(s: "Goodbye")}work()

When work() is called, the console gives the following output:

[A s:Hello] init[B a:[A s:Hello]] init[B a:[A s:Hello]][A s:Hello] deinit[B a:nil][A s:Goodbye] init[B a:nil] willSet a[B a:[A s:Goodbye]] didSet a[A s:Goodbye] deinit[B a:nil] deinit

Notice that in neither case of the instance of A deallocating and its weak reference in the instance of B becoming nil are the property observers called. Only in the direct case of assignment to B.a are they called.