Observable

@propertyWrapper
public class Observable<T>

An object that allows to observe changes mades to a value that it holds.

Properties

  • The underlying value referenced by the observable.

    Declaration

    Swift

    public var wrappedValue: T { get set }

Initialization

  • Creates and returns a new observable with passed initial value

    Declaration

    Swift

    public init(_ value: T)

    Parameters

    value

    The initial value

Observe

  • Observe (subscribe) to the value changes that returns cancellable

    Declaration

    Swift

    @discardableResult
    public func observe(handler: @escaping (T) -> Void) -> ObserverCancellable

    Parameters

    handler

    An handler that will be called on every value change. It passes the changed value.

    Return Value

    A cancellable object that allows to stop observation

  • Observe (subscribe) to the value changes with an owner object that is used to hold the subscription as long as the owner is not deallocated.

    Declaration

    Swift

    public func observe<O>(owner: O, handler: @escaping (O, T) -> Void) where O : AnyObject

    Parameters

    owner

    An owner object that is used to keep the observation alive

    handler

    An handler that will be called on each value change. It passes the weakified owner and the value.