Skip to content

Instantly share code, notes, and snippets.

@nikolaykasyanov
Created April 3, 2019 08:13
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 nikolaykasyanov/2042c2a12253d2029826ce308d143ac3 to your computer and use it in GitHub Desktop.
Save nikolaykasyanov/2042c2a12253d2029826ce308d143ac3 to your computer and use it in GitHub Desktop.
import Foundation
class Child {
deinit {
print("Child.deinit")
}
var parent: Parent?
func removeFromParent() {
defer {
print("removed myself from parent")
}
print("going to remove myself from parent...")
autoreleasepool {
parent?.children.removeAll { $0 === self }
parent = nil
}
}
}
class Parent {
var children: [Child] = []
func add(child: Child) {
children.append(child)
child.parent = self
}
}
func setup() -> Parent {
return autoreleasepool {
let parent = Parent()
let child = Child()
parent.add(child: child)
return parent
}
}
let root = setup()
root.children[0].removeFromParent()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment