Created
September 13, 2018 14:25
-
-
Save DevAndArtist/c6ced51acee7a6dc35b1952ead1f6208 to your computer and use it in GitHub Desktop.
Deinit experiement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ref { | |
weak var obj: Obj? { | |
didSet { print("didSet", obj as Any) } | |
} | |
} | |
class Obj { | |
weak var ref: Ref? | |
func unlink(from ref: Ref) { | |
print(ref.obj as Any) | |
} | |
deinit { | |
print("deinit") | |
ref.map(unlink(from:)) | |
} | |
} | |
var obj: Obj? = Obj() | |
let ref = Ref() | |
ref.obj = obj | |
print(obj?.ref as Any) | |
obj = nil | |
/* | |
* Conclusion: | |
* If an object calls `deinit` it means that if it's instance | |
* contains a reference to an other object that previously | |
* referred to the current instance, it will be `nil`. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment