KVO Introduction
Key-value observing is a mechanism that allows objects to be notified of changes to specified properties of other objects.
KVO 是一种机制:当其它对象的特定属性改变时会通知某个对象。
At a Glance
It is particularly useful for communication between model and controller layers in an application.
KVO 在 Model 和 controller 通信很有用。
A controller object typically observes properties of model objects, and a view object observes properties of model objects through a controller.
controller 对象通常观察 model 的属性,view 对象通过 controller 观察 model 的属性。
In addition, however, a model object may observe other model objects (usually to determine when a dependent value changes) or even itself (again to determine when a dependent value changes).
model 也可以观察其他对象的属性,甚至也可以观察自己。
You can observe 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.
观察的属性包括:attribute,to-one relationships,to-many relationships。
To use KVO, first you must ensure that the observed object, the Account in this case, is KVO compliant. Typically, if your objects inherit from NSObject and you create properties in the usual way, your objects and their properties will automatically be KVO Compliant. It is also possible to implement compliance manually.
首先确定被观察者支持 KVO。通常,若对象继承自 NSObject,且通过常规的方式创建属性,那么你的对象和属性则自动支持 KVO。当然,也可以手动实现 KVO 支持。
Next, you must register your observer instance, the Person, with the observed instance, the Account.
其次,需要把观察者注册到被观察者的 keypath 上。
In order to receive change notifications from the Account, Person implements the observeValueForKeyPath:ofObject:change:context: method, required of all observers. The Account will send this message to the Person any time one of the registered key paths changes.
为了接收到通知,观察者需要实现 observeValueForKeyPath:ofObject:change:context。
被观察者注册的 key paths 改变,观察者就会收到通知。
Finally, when it no longer wants notifications, and at the very least before it is deallocated, the Person instance must de-register by sending the message removeObserver:forKeyPath: to the Account.
最后,当不再需要通知时,需要至少在观察者 dealloc 之前通过 removeObserver:forKeyPath: 移除注册。
KVO’s primary benefit is that you don’t have to implement your own scheme to send notifications every time a property changes. Its well-defined infrastructure has framework-level support that makes it easy to adopt—typically you do not have to add any code to your project. In addition, the infrastructure is already full-featured, which makes it easy to support multiple observers for a single property, as well as dependent values.
KVO 最大的好处是:不用添加任何代码可以实现属性发生变化时发送通知给观察者,同时它也是一个系统框架级的基础结构,更容易被人接受。另外,KVO 功能强大,支持多个观察者注册一个属性,也支持属性值之间的依赖。
Registering Dependent Keys explains how to specify that the value of a key is dependent on the value of another key.
Registering Dependent Keys 这一节讲述了如何指定一个 key 的 value 依赖于另一个 key 的 value。
Unlike notifications that use NSNotificationCenter, there is no central object that provides change notification for all observers. Instead, notifications are sent directly to the observing objects when changes are made. NSObject provides this base implementation of key-value observing, and you should rarely need to override these methods.
KVO 不同于 NSNotificationCenter,不需要中间对象转发通知,而是通知直接发送给观察者对象。
Key-Value Observing Implementation Details describes how key-value observing is implemented.
Key-Value Observing Implementation Details 这一节描述了 KVO 是如何实现的。