w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Zeroing weak variable and deinit, which one happens first?

Zeroing weak variable happens first. deinit happens later. At least in current implementation (Xcode 6.1, Swift 1.1) This is a result of observation of specific implementation, and I don't know how it is actually defined by the authors... If you have explicit source, please comment or answer.

There's also a related discussion in ADC forum.

Test code Avoid Playground when testing this to get correct lifecycle behaviour.

class AAA {
    func test() {
    }
}

var         a1  =   nil as AAA?
weak var    a2  =   nil as AAA?

class BBB: AAA {
    var data    =   "Here be dragons."
    override func test() {
        println("test() called and a2 is now (a2).")
    }
    deinit {
        println("deinit called and a2 is now (a2).")
    }
}

a1  =   BBB()
a2  =   a1
a2!.test()

a1  =   nil

Result:

test() called and a2 is now
Optional(weak_deinit_order_comparison.BBB).
deinit called and a2 is now nil.

Then, the weak variable becomes nil before the deinit to be called.

Update

This pre-nilling is applied equally to unowned objects. Unowned object will become inaccessible at the point of deist just like weak, and trial to access unowned object at the deinit will crash the app.

Update 2

If you assign self to a weak var variable in deinit, it will become nil immediately. (Xcode Version 6.3.2 (6D2105))

class Foo {
    init() {

    }
    deinit {
        var         a   =   self
        weak var    b   =   self
        unowned var c   =   self
        let         d   =   Unmanaged.passUnretained(self)

        println(a)  // prints `Foo`.
        println(b)  // prints `nil`.
//      println(c)  // crashes.
        println(d.takeUnretainedValue()) // prints `Foo`.
    }
}


var f   =   Foo() as Foo?
f       =   nil




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.