Created
September 12, 2019 10:03
-
-
Save azonov/96c8a54cae75e14f21ec5c2a37f5e22f to your computer and use it in GitHub Desktop.
Weak self capturing in Swift closures
This file contains 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
func weak<T: AnyObject>(_ obj: T, _ block: @escaping (T) -> () -> Void) -> () -> Void { | |
return { [weak obj] in | |
obj.map(block)?() | |
} | |
} | |
func weak<T: AnyObject, Argument>(_ obj: T, _ block: @escaping (T) -> (Argument) -> Void) -> (Argument) -> Void { | |
return { [weak obj] a in | |
obj.map(block)?(a) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Usage
// Without weak