Skip to content

Instantly share code, notes, and snippets.

@zats
Last active January 4, 2016 18:02
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 zats/c6f11af024f02bd217ab to your computer and use it in GitHub Desktop.
Save zats/c6f11af024f02bd217ab to your computer and use it in GitHub Desktop.
Delegate-pattern without a weak delegate object creates retain cycle
class Parent {
var child: Child? {
didSet {
child.callback = delegateCallback // Fix: child.callback = weakify(self, Parent.delegateCallback) using https://github.com/klundberg/Weakify/blob/master/Weakify/Weakify.swift#L15-L21
}
}
func delegateCallback() {
// ...
}
}
class Child {
var callback: (Void -> Void)?
private func triggerCallback() {
callback?()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment