Last active
January 4, 2016 18:02
-
-
Save zats/c6f11af024f02bd217ab to your computer and use it in GitHub Desktop.
Delegate-pattern without a weak delegate object creates retain cycle
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 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