Observer

public protocol Observer

Definition of an object for adding KVO and NSNotificationCenter obeservations to pure Swift classes.

NSObjects implement a method called methodSignatureForSelector: which converts from strings to methods. Pure Swift classes do not have this method so cannot inherently perform KVO or observe NSNotificationCenter posts. Rather than making a Swift class inherit from NSObject, which introuduces unnecessary features, an Observer can be retained which performs an arbitrary block on copmletion.

Two default implementations are provided, NotificationObserver and ObjectObserver for NSNotificationCenter observing and keypath observing respectively.

  • Should be called by the user of the Observer to register as an observer

    Declaration

    Swift

    func beginObserving()
  • Should be called by the user of the Observer to unregister as an observer.

    Declaration

    Swift

    func endObserving()