What is KVO (Key-Value Observation)?

KVO is one of the core parts of Cocoa Touch and is used widely across the platform.

KVO stands for Key-Value Observation and provides me- chanics through which you can observe changes on properties in iOS. In con- trast to Delegate KVO entails a one-to-many relationship. Multiple objects could subscribe to changes in a property of another object. As soon as that property changes, all objects subscribing to it will be notified.

Under-the-hood implementation uses instance variables defined with properties to store the actual value of the property and setters/getters supplied by synthe- sization of those properties. Internally, when you assign a property it will call willChangeValueForKey: and didChangeValueForKey: to trigger the change broadcast to observers.

Another way that KVO is used in iOS apps is public broadcasting of messages through NSNotificationCenter. The underlying mechanics are the same as with property KVO but the broadcasting can be triggered via a post: method on NSNotificationCenter default center rather than a property change.

Originally an Objective-C feature, this is also available in Swift to classes sub- classed from NSObject.

KVO on its own is a fairly bulky technology but it opens up a lot of possibilities that you can build on. There are a lot of great FRP projects like ReactiveCocoa and RxSwift that were built using KVO mechanics.