NSKeyValueObserving

An informal protocol that objects adopt to be notified of changes to the specified properties of other objects.

Overview

You can observe any object properties including simple attributes, to-one relationships, and to-many relationships. Observers of to-many relationships are informed of the type of change made — as well as which objects are involved in the change.

NSObject provides an implementation of the NSKeyValueObserving protocol that provides an automatic observing capability for all objects. You can further refine notifications by disabling automatic observer notifications and implementing manual notifications using the methods in this protocol.

Topics

Change Notification

- observeValueForKeyPath:ofObject:change:context:

Informs the observing object when the value at the specified key path relative to the observed object has changed.

Registering for Observation

- addObserver:forKeyPath:options:context:

Registers the observer object to receive KVO notifications for the key path relative to the object receiving this message.

- removeObserver:forKeyPath:

Stops the observer object from receiving change notifications for the property specified by the key path relative to the object receiving this message.

- removeObserver:forKeyPath:context:

Stops the observer object from receiving change notifications for the property specified by the key path relative to the object receiving this message, given the context.

Notifying Observers of Changes

- willChangeValueForKey:

Informs the observed object that the value of a given property is about to change.

- didChangeValueForKey:

Informs the observed object that the value of a given property has changed.

- willChange:valuesAtIndexes:forKey:

Informs the observed object that the specified change is about to be executed at given indexes for a specified ordered to-many relationship.

- didChange:valuesAtIndexes:forKey:

Informs the observed object that the specified change has occurred on the indexes for a specified ordered to-many relationship.

- willChangeValueForKey:withSetMutation:usingObjects:

Informs the observed object that the specified change is about to be made to a specified unordered to-many relationship.

- didChangeValueForKey:withSetMutation:usingObjects:

Informs the observed object that the specified change was made to a specified unordered to-many relationship.

Observing Customization

+ automaticallyNotifiesObserversForKey:

Returns a Boolean value that indicates whether the observed object supports automatic key-value observation for the given key.

+ keyPathsForValuesAffectingValueForKey:

Returns a set of key paths for properties whose values affect the value of the specified key.

+ setKeys:triggerChangeNotificationsForDependentKey:

Configures the observed object to post change notifications for a given property if any of the properties specified in a given array changes.

Deprecated
observationInfo

Returns a pointer that identifies information about all of the observers that are registered with the observed object.

Constants

NSKeyValueChange

The kinds of changes that can be observed.

NSKeyValueObservingOptions

The values that can be returned in a change dictionary.

NSKeyValueChangeKey

The keys that can appear in the change dictionary.

NSKeyValueSetMutationKind

The kinds of mutation that you can make to an unordered collection.

See Also