美文网首页iOS学习笔记
KVO官方文档学习(一)----简介

KVO官方文档学习(一)----简介

作者: 郝嗨森 | 来源:发表于2019-01-24 15:41 被阅读0次

    KVO官方文档

    KVO一瞥

    Key-value observing provides a mechanism that allows objects to be notified of changes to specific properties of other objects. It is particularly useful for communication between model and controller layers in an application. (In OS X, the controller layer binding technology relies heavily on key-value observing.) A controller object typically observes properties of model objects, and a view object observes properties of model objects through a controller. 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).

    KVO提供了一种机制,允许对象收到其他对象的特定属性改变的通知。
    在应用程序中模型和控制器层之间的通信尤其有用。(在OS X中,控制器层绑定技术严重依赖KVO)。控制器对象通常观察模型对象的属性,视图对象通过控制器观察模型对象的属性。另外,然而,一个模型对象可以观察其他模型对象(通常用来确定依赖值何时改变)甚至自己(再次确定依赖值何时改变)。

    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.

    你可以观察属性,包括简单属性,一对一关系和一对多关系。一对多关系的观察者被告知所做出的变化类型以及变化中涉及的对象。

    A simple example illustrates how KVO can be useful in your application. Suppose a Person object interacts with an Account object, representing the person’s savings account at a bank. An instance of Person may need to be aware of when certain aspects of the Account instance change, such as the balance, or the interest rate.

    一个简单的例子说明了KVO在你的应用程序中是如何发挥作用的。假设Person对象和Account对象交互,表示一个人在银行的储蓄账户。Person的实例可能需要知道Account实例某些方面何时发生变化,比如余额或者利率。

    kvo_objects_properties.png

    If these attributes are public properties of Account, the Person could periodically poll the Account to discover changes, but this is of course inefficient, and often impractical. A better approach is to use KVO, which is akin to Person receiving an interrupt when a change occurs.

    如果这些属性时Account的公开属性,Person可以定期轮询Account来发现变化,但是这样是低效的,而且通常不切实际。一个更好的方法时使用KVO,类似于当变化发生时Person接收中断。

    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 Compliance describes the difference between automatic and manual key-value observing, and how to implement both.

    要使用KVO,首先必须确定被观察的对象(这个例子中的Account)符合KVO。通常,如果你的对象继承自NSObject而且使用常规方式创建属性,你的对象和他们的属性将自动遵从KVO,也可以手动实现。 KVO Compliance介绍了自动和手动KVO的不同以及如何实现它们。

    Next, you must register your observer instance, the Person, with the observed instance, the Account. Person sends an addObserver:forKeyPath:options:context: message to the Account, once for each observed key path, naming itself as the observer.

    接下来,必须通过被观察的实例(Account)注册观察者实例(Person)。PersonAccount发送addObserver:forKeyPath:options:context:消息,每个被观察的键发送一次,将它自己命名为观察者。

    kvo_objects_add.png

    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. The Person can then take appropriate action based upon the change notification.

    为了从Account接受变化的通知,Person实现了所有观察者必需实现的方法observeValueForKeyPath:ofObject:change:context: 。只要其中一个注册的键路径发生变化,Account就会把这个消息发送给Person。然后,Person可以根据变化通知采取适当的操作。

    kvo_objects_observe.png

    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.

    最后,当不想要接收通知时,并且至少在它被销毁之前,Person实例必须通过向Account发送消息removeObserver:forKeyPath:来取消注册。

    kvo_objects_remove.png

    个人总结:
    1.KVO是一种观察者机制,允许一个对象观察其他对象的属性的变化。
    2.使用KVO整体分为四步:

    • 确定被观察者对象允许使用KVO(对象继承自NSObject并且通过setter或KVC对属性赋值)
    • 注册观察者对象
    • 观察者实现回调方法
    • 观察者移除观察

    相关文章

      网友评论

        本文标题:KVO官方文档学习(一)----简介

        本文链接:https://www.haomeiwen.com/subject/cdgndqtx.html