Skip to content

Instantly share code, notes, and snippets.

@knowsudhanshu
Last active September 27, 2021 03:27
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 knowsudhanshu/44247078cacfd409d481ff7fb81c1e3d to your computer and use it in GitHub Desktop.
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)
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