Observable
public class Observable<T>
The GGNObservable class can be used for simple reactive style programming. This class has a generic type constraint.
-
This typealias exists mainly for convenience, however it’s type is important.
Closureis anoptionalfunction that takes an instance of the generic typeTand returnsVoid.Returns
VoidDeclaration
Swift
public typealias Closure = ((T) -> Void)Parameters
TA function that takes a
TReturn Value
Void -
Initializes an instance of an
Obersvablewith a generic typeT.- Example:
let alertOutput = Observable<UIAlertController>()
Declaration
Swift
public init() - Example:
-
Call this method on an instance of
Observableto respond to events emitted from it. This method captures the passed in closure, stores it, and performs it whenemit(event:)is called on the same instance ofObservable. OneObservablecan have multiple observers.Example:
viewModel.alertOutput.onNext { [weak self] alert in self?.presentViewController(alert, animated: true, completion: nil) }
Declaration
Swift
public func onNext(perform closure: Closure)Parameters
closureThe closure to perform on
emit(event:). -
Call this method on an instance of
Observableto emit an instance of the generic typeT. This method performs any and all closures that are captured by calls toonNext(perform:).Example:
alertOutput.emit(alert)
Declaration
Swift
public func emit(event: T)Parameters
eventAn instance of the generic type
T.
Observable Class Reference