addObserver method

Observation addObserver(
  1. Observer observer, {
  2. required NSString forKeyPath,
  3. NSKeyValueObservingOptions options = NSKeyValueObservingOptions.NSKeyValueObservingOptionNew,
  4. Pointer<Void>? context,
})

Registers the Observer to receive KVO notifications for the key path relative to this NSObject.

Returns an Observation, which must be held onto for as long as KVO notifications are required. The Observation keeps the Observer and the observed NSObject alive.

This method wraps ObjC's addObserver:forKeyPath:options:context: method. However there is no matching removeObserver method, as Observation.remove serves that purpose.

Implementation

Observation addObserver(
  Observer observer, {
  required NSString forKeyPath,
  NSKeyValueObservingOptions options =
      NSKeyValueObservingOptions.NSKeyValueObservingOptionNew,
  Pointer<Void>? context,
}) => Observation._(
  DOBJCObservation().initForKeyPath(
    forKeyPath,
    ofObject: this,
    withObserver: observer,
    options: options,
    context: context ?? nullptr,
  ),
);