Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
An auto-weak delegate for handle modern delegate pattern.
public class Delegate<Input, Output> {
public init() {}
private var block: ((Input) -> Output?)?
public func delegate<T: AnyObject>(on target: T, block: ((T, Input) -> Output)?) {
self.block = { [weak target] input in
guard let target = target else { return nil }
return block?(target, input)
public func call(_ input: Input) -> Output? {
return block?(input)
public extension Delegate where Input == Void {
func call() -> Output? {
return call(())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment