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.
Closure
is anoptional
function that takes an instance of the generic typeT
and returnsVoid
.Returns
Void
Declaration
Swift
public typealias Closure = ((T) -> Void)
Parameters
T
A function that takes a
T
Return Value
Void
-
Initializes an instance of an
Obersvable
with a generic typeT
.- Example:
let alertOutput = Observable<UIAlertController>()
Declaration
Swift
public init()
- Example:
-
Call this method on an instance of
Observable
to 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
. OneObservable
can 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
closure
The closure to perform on
emit(event:)
. -
Call this method on an instance of
Observable
to 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
event
An instance of the generic type
T
.