Skip to content

Instantly share code, notes, and snippets.

@DevAndArtist
Created September 13, 2018 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DevAndArtist/c6ced51acee7a6dc35b1952ead1f6208 to your computer and use it in GitHub Desktop.
Save DevAndArtist/c6ced51acee7a6dc35b1952ead1f6208 to your computer and use it in GitHub Desktop.
Deinit experiement
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