Last active
September 27, 2021 03:27
-
-
Save knowsudhanshu/44247078cacfd409d481ff7fb81c1e3d to your computer and use it in GitHub Desktop.
Wikifiable: enables getting rid of the boilerplate code in an escaping closure where there is a repeated code [weak self] ( Credit: Vincent Pradeilles)
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
protocol Weakifiable where Self: class {} | |
extension NSObject: Weakifiable {} | |
extension Weakifiable { | |
func weakify<T>(_ code: @escaping (Self, T) -> Void) -> (T) -> Void { | |
{ | |
[weak self] (data) in | |
guard let self = self else { return } | |
code(self, data | |
} | |
} | |
} | |
// Ref: https://www.youtube.com/watch?v=BGzPK7f13RM |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment