Skip to content

Instantly share code, notes, and snippets.

@xxKRASHxx
Created January 11, 2018 14:02
Show Gist options
  • Save xxKRASHxx/f3f61434155ed03ad1fb2365bb9b0075 to your computer and use it in GitHub Desktop.
Save xxKRASHxx/f3f61434155ed03ad1fb2365bb9b0075 to your computer and use it in GitHub Desktop.
public func weakify<Value: AnyObject, Arguments, Result>(
_ function: @escaping (Value) -> (Arguments) -> Result,
object: Value,
default value: Result)
-> (Arguments) -> Result {
return { [weak object] arguments in
object.map { function($0)(arguments) } ?? value
}
}
public func weakify<Value: AnyObject, Arguments>(
_ function: @escaping (Value) -> (Arguments) -> Void,
object: Value)
-> (Arguments) -> Void {
return weakify(function, object: object, default: ())
}
public func weakify<Value: AnyObject>(
_ function: @escaping (Value) -> () -> Void,
object: Value)
-> () -> Void {
return { [weak object] in
object.map { function($0)() }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment