Notify objects about changes to the properties of other objects.
Overview
Key-value observing is a Cocoa programming pattern you use to notify objects about changes to properties of other objects. It's useful for communicating changes between logically separated parts of your app—such as between models and views. You can only use key-value observing with classes that inherit from NSObject.
Annotate a Property for Key-Value Observing
Mark properties that you want to observe through key-value observing with both the @objc attribute and the dynamic modifier. The example below defines the My class with a property—my—that can be observed:
class MyObjectToObserve: NSObject {
@objc dynamic var myDate = NSDate(timeIntervalSince1970: 0) // 1970
func updateDate() {
myDate = myDate.addingTimeInterval(Double(2 << 30)) // Adds about 68 years.
}
}
Define an Observer
An instance of an observer class manages information about changes made to one or more properties. When you create an observer, you start observation by calling the observe(_: method with a key path that refers to the property you want to observe.
In the example below, the \.object key path refers to the my property of My:
class MyObserver: NSObject {
@objc var objectToObserve: MyObjectToObserve
var observation: NSKeyValueObservation?
init(object: MyObjectToObserve) {
objectToObserve = object
super.init()
observation = observe(
\.objectToObserve.myDate,
options: [.old, .new]
) { object, change in
print("myDate changed from: \(change.oldValue!), updated to: \(change.newValue!)")
}
}
}
You use the old and new properties of the NSKey instance to see what's changed about the property you're observing.
If you don't need to know how a property has changed, omit the options parameter. Omitting the options parameter forgoes storing the new and old property values, which causes the old and new properties to be nil.
Associate the Observer with the Property to Observe
You associate the property you want to observe with its observer by passing the object to the initializer of the observer:
let observed = MyObjectToObserve()
let observer = MyObserver(object: observed)
Respond to a Property Change
Objects that are set up to use key-value observing—such as observed above—notify their observers about property changes. The example below changes the my property by calling the update method. That method call automatically triggers the observer's change handler:
observed.updateDate() // Triggers the observer's change handler.
// Prints "myDate changed from: 1970-01-01 00:00:00 +0000, updated to: 2038-01-19 03:14:08 +0000"
The example above responds to the property change by printing both the new and old values of the date.