Kingfisher Documentation

Class Delegate

public class Delegate<Input, Output>  

A class that keeps a weakly reference for self when implementing onXXX behaviors. Instead of remembering to keep self as weak in a stored closure:

// MyClass.swift
var onDone: (() -> Void)?
func done() {
    onDone?()
}

// ViewController.swift
var obj: MyClass?

func doSomething() {
    obj = MyClass()
    obj!.onDone = { [weak self] in
        self?.reportDone()
    }
}

You can create a Delegate and observe on self. Now, there is no retain cycle inside:

// MyClass.swift
let onDone = Delegate<(), Void>()
func done() {
    onDone.call()
}

// ViewController.swift
var obj: MyClass?

func doSomething() {
    obj = MyClass()
    obj!.onDone.delegate(on: self) { (self, _)
        // `self` here is shadowed and does not keep a strong ref.
        // So you can release both `MyClass` instance and `ViewController` instance.
        self.reportDone()
    }
}

Initializers

init()

public init()  

Methods

delegate(on:​block:​)

public func delegate<T: AnyObject>(on target: T, block: ((T, Input) -> Output)?)  

call(_:​)

public func call(_ input: Input) -> Output?  

call​AsFunction(_:​)

public func callAsFunction(_ input: Input) -> Output?