Key-Value Observing Programming Guide 阅读笔记

Key-value observing provides a mechanism that allows objects to be notified of changes to specific properties of other objects.A controller object typically observes properties of model objects, and a view object observes properties of model objects through a controller.

class 必须首先遵从 NSKeyValueCoding 、然后是 NSKeyValueObserving

Registering for Key-Value Observing

Receiving Notification of a Change

Removing an Object as an Observer

You must perform the following steps to enable an object to receive key-value observing notifications for a KVO-compliant property:

//Register the observer with the observed object
//passing itself as the observer and the key path of the property to be observed. 
//The observer additionally specifies an options parameter and a context pointer to manage aspects of the notifications.
//A safer and more extensible approach is to use the context to ensure notifications you receive are destined for your observer and not a superclass.
- addObserver:forKeyPath:options:context:
//accept change notification messages
- observeValueForKeyPath:ofObject:change:context:
//Unregister the observer,At a minimum, invoke this method before the observer is released from memory.
- removeObserver:forKeyPath:

注意:The key-value observing addObserver:forKeyPath:options:context:method does not maintain strong references to the observing object, the observed objects, or the context.

注册监听器:

- addObserver:forKeyPath:options:context: 注册一个监听器用于监听指定Key路径
- removeObserver:forKeyPath: 为指定Key路径删除指定的监听器
- removeObserver:forKeyPath:context: 为指定Key路径删除指定的监听器,只是多了一个context参数。

监听器开始观察:

- observeValueForKeyPath:ofObject:change:context:

- (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key 

返回yes(默认返回YES),不用调用willChangeValueForKey
返回no,在改变被观察的属性值时,需要自己调用willChangeValueForKey 或 didChangeValueForKey 方法

如果不使用存取器,需要在每次修改属性的实例变量时调用 willChangeValueForKey: 和 调用 didChangeValueForKey:

Key-Value Observing Implementation Details

Automatic key-value observing is implemented using a technique called isa-swizzling.

kvo实现原理图

The isa pointer, as the name suggests, points to the object's class which maintains a dispatch table. This dispatch table essentially contains pointers to the methods the class implements, among other data.

When an observer is registered for an attribute of an object the isa pointer of the observed object is modified, pointing to an intermediate class rather than at the true class. As a result the value of the isa pointer does not necessarily reflect the actual class of the instance.

You should never rely on the isa pointer to determine class membership. Instead, you should use the class method to determine the class of an object instance.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,868评论 0 23
  • “一切福田,都离不开心地。”这句话类似于“吃亏是福”,意思就是,在心田上播下善良的种子,总有一天,它会开花结果。这...
    糖小妹阅读 345评论 0 0
  • 就像一出戏,有开始也有结束。我们既是演戏的,也是看戏的。看他人戏里戏外,演自己的千秋风云。何人欢呼,谁人在意...
    山猫公子阅读 147评论 0 2
  • 坊间传闻“土嗨”最早收录在杀皇二世强子的火星法典上: ←辷個仒の圡嗨,兩個仒の徣,⒊天⒊液の放倊燃燒钌漃寞√ 很明...
    一本正经得扯淡阅读 529评论 1 1
  • 回首刚刚过去的2016,最宝贵的收获可能在于接触了投资理财这个领域。 2016年是进入到现在这个体制内单位的第4年...
    地心阅读 621评论 0 50