I have the following dummy class:
class Test {
var value: String
init(value: String) {
self.value = value
}
deinit {
print("deinit!")
}
}
And I have the following code in a Swift Playground:
var strongReference: Test? = Test(value: "test")
weak var variable: Test!
variable = strongReference
strongReference = nil
print(variable.value)
This code actually works and prints "Test" and after that "deinit!". This comes as a surprise for me because I was expecting a crash in this situation. Even more stranger, the following code actually generates the crash I was looking for:
var strongReference: Test? = Test(value: "test")
weak var variable: Test! = strongReference
strongReference = nil
print(variable.value)
Anyone knows why is this happening?