当前位置:   article > 正文

iOS开发笔记--详解苹果的黑魔法 – KVO 的奥秘

iOS开发笔记--详解苹果的黑魔法 – KVO 的奥秘

序言

在iOS开发中,苹果提供了许多机制给我们进行回调。KVO(key-value-observing)是一种十分有趣的回调机制,在某个对象注册监听者后,在被监听的对象发生改变时,对象会发送一个通知给监听者,以便监听者执行回调操作。最常见的KVO运用是监听scrollView的contentOffset属性,来完成用户滚动时动态改变某些控件的属性实现效果,包括渐变导航栏、下拉刷新控件等效果。

这里写图片描述

使用

KVO的使用非常简单,使用KVO的要求是对象必须能支持kvc机制——所有NSObject的子类都支持这个机制。拿上面的渐变导航栏做,我们为tableView添加了一个监听者controller,在我们滑动列表的时候,会计算当前列表的滚动偏移量,然后改变导航栏的背景色透明度。

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//添加监听者</span>
[<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.tableView</span> addObserver: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> forKeyPath: @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"contentOffset"</span> options: NSKeyValueObservingOptionNew context: <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/**
 *  监听属性值发生改变时回调
 */</span>
- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)observeValueForKeyPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)keyPath ofObject:(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>)object change:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span><<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>> *)change context:(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> *)context
{
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">CGFloat</span> offset = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.tableView</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.contentOffset</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.y</span>;
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">CGFloat</span> delta = offset / <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">64.</span>f + <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.</span>f;
    delta = MAX(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>, delta);
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> alphaNavController]<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.barAlpha</span> = MIN(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>, delta);
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li></ul>

毫无疑问,kvo是一种非常便捷的回调方式,但是编译器是怎么完成监听这个任务的呢?先来看看苹果文档对于KVO的实现描述

Automatic key-value observing is implemented using a technique called isa-swizzling… 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 ..

简要的来说,在我们对某个对象完成监听的注册后,编译器会修改监听对象(上文中的tableView)的isa指针,让这个指针指向一个新生成的中间类。从某个意义上来说,这是一场骗局。

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> objc_class *Class;
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">typedef</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> objc_object {
   Class isa;
} *<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>;</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li></ul>

这里要说明的是isa这个指针,isa是一个Class类型的指针,用来指向类的类型,我们可以通过object_getClass方法来获取这个对象的元类,即是类对象的类型(正常来说,class方法内部的实现就是获取这个isa指针,但是在kvo中苹果对监听对象的这个方法进行了重写。之前这里描述有误,说成是指向父类,多谢夏都为我纠正)。而Class方法是获得对象的类型,虽然这两个返回的结果是一样的,但是两个方法本质上得出的结果不是同一个东西 
在oc中,规定了只要拥有isa指针的变量,通通都属于对象。上面的objc_object表示的是NSObject这个类的结构体表示,因此oc不允许出现非NSObject子类的对象(block是一个特殊的例外) 
当然了,苹果并不想讲述更多的实现细节,但是我们可以通过运行时机制来完成一些有趣的调试。

苹果的黑魔法

根据苹果的说法,在对象完成监听注册后,修改了被监听对象的某些属性,并且改变了isa指针,那么我们可以在监听前后输出被监听对象的相关属性来进一步探索kvo的原理。为了保证能够得到对象的真实类型,我使用了object_getClass方法,这个方法在runtime.h头文件中

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"address: %p"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.tableView</span>);
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"class method: %@"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.tableView</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.class</span>);
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"description method: %@"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.tableView</span>);
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"use runtime to get class: %@"</span>, object_getClass(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.tableView</span>));
[<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.tableView</span> addObserver: <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> forKeyPath: @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"contentOffset"</span> options: NSKeyValueObservingOptionNew context: <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"==================================================="</span>);
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"address: %p"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.tableView</span>);
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"class method: %@"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.tableView</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.class</span>);
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"description method: %@"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.tableView</span>);
<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"use runtime to get class %@"</span>, object_getClass(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.tableView</span>));</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li></ul>

在看官们运行这段代码之前,可以先思考一下上面的代码会输出什么。

<code class="hljs oxygene has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33.216</span> LXDAlphaNavigationController[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1487</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">63171</span>] address: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>x7f927a81d200
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33.216</span> LXDAlphaNavigationController[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1487</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">63171</span>] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">method</span>:</span> UITableView
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33.217</span> LXDAlphaNavigationController[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1487</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">63171</span>] description <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">method</span>:</span> <UITableView: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>x7f927a81d200; frame = (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">320</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">568</span>); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>x7f927971f9a0>; layer = <CALayer: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>x7f9279706f50>; contentOffset: <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">{0, 0}</span>; contentSize: <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">{600, 0}</span>>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33.217</span> LXDAlphaNavigationController[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1487</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">63171</span>] use runtime <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> get <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span>: UITableView
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33.217</span> LXDAlphaNavigationController[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1487</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">63171</span>] ===================================================
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33.218</span> LXDAlphaNavigationController[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1487</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">63171</span>] address: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>x7f927a81d200
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33.218</span> LXDAlphaNavigationController[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1487</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">63171</span>] <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">method</span>:</span> UITableView
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33.218</span> LXDAlphaNavigationController[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1487</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">63171</span>] description <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">method</span>:</span> <UITableView: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>x7f927a81d200; frame = (<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">320</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">568</span>); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>x7f927971f9a0>; layer = <CALayer: <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>x7f9279706f50>; contentOffset: <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">{0, 0}</span>; contentSize: <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">{600, 0}</span>>
<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2015</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span>-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">12</span> <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">23</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">02</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">33.230</span> LXDAlphaNavigationController[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1487</span>:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">63171</span>] use runtime <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">to</span> get <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span> NSKVONotifying_UITableView</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li></ul>

除了通过object_getClass获取的类型之外,其他的输出没有任何变化。class方法跟description方法可以重写实现上面的效果,但是为什么连地址都是一样的。 
这里可以通过一句小代码来说明一下:

<code class="hljs ruby has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-constant" style="box-sizing: border-box;">NSLog</span>(@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%@, %@"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span>.<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span>);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>

上面这段代码不管你怎么输出,两个结果都是一样的。这是由于super本质上指向的是父类内存。这话说起来有点绕口,但是我们可以通过对象内存图来表示: 
类的内存
每一个对象占用的内存中,一部分是父类属性占用的;在父类占用的内存中,又有一部分是父类的父类占用的。前文已经说过isa指针指向的是父类,因此在这个图中,Son的地址从Father开始,Father的地址从NSObject开始,这三个对象内存的地址都是一样的。通过这个,我们可以猜到苹果文档中所提及的中间类就是被监听对象的子类。并且为了隐藏实现,苹果还重写了这个子类的class方法跟description方法来掩人耳目。另外,我们还看到了新类相对于父类添加了一个NSKVONotifying_前缀,添加这个前缀是为了避免多次创建监听子类,节省资源

怎么实现类似效果

既然知道了苹果的实现过程,那么我们可以自己动手通过运行时机制来实现KVO。runtime允许我们在程序运行时动态的创建新类、拓展方法、method-swizzling、绑定属性等等这些有趣的事情。 
在创建新类之前,我们应该学习苹果的做法,判断当前是否存在这个类,如果不存在我们再进行创建,并且重新实现这个新类的class方法来掩盖具体实现。基于这些原则,我们用下面的方法来获取新类

<code class="hljs oxygene has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Class</span>)createKVOClassWithOriginalClassName: (NSString *)className
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">{
    NSString * kvoClassName = [kLXDkvoClassPrefix stringByAppendingString: className];
    Class observedClass = NSClassFromString(kvoClassName);
    if (observedClass) { return observedClass; }</span>

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//创建新类,并且添加LXDObserver_为类名新前缀</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Class</span> originalClass = object_getClass(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>);
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Class</span> kvoClass = objc_allocateClassPair(originalClass, kvoClassName.UTF8String, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>);

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取监听对象的class方法实现代码,然后替换新建类的class实现</span>
    <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Method</span> <span class="hljs-title" style="box-sizing: border-box;">classMethod</span> = <span class="hljs-title" style="box-sizing: border-box;">class_getInstanceMethod</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(originalClass, @<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">selector</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span>)</span>);</span>
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> char * types = method_getTypeEncoding(classMethod);
    class_addMethod(kvoClass, @<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">selector</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span>), (IMP)kvo_Class, types);
    objc_registerClassPair(kvoClass);
    return kvoClass;
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>

另外,在判断是否需要中间类来完成监听的注册前,我们还要判断监听的属性的有效性。通过获取变量的setter方法名(将首字母大写并加上前缀set),以此来获取setter实现,如果不存在实现代码,则抛出异常使程序崩溃。

<code class="hljs oxygene has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">SEL setterSelector = NSSelectorFromString(setterForGetter(key));
<span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Method</span> <span class="hljs-title" style="box-sizing: border-box;">setterMethod</span> = <span class="hljs-title" style="box-sizing: border-box;">class_getInstanceMethod</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">([<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">class</span>], setterSelector)</span>;</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!setterMethod) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">{
    @throw [NSException exceptionWithName: NSInvalidArgumentException reason: [NSString stringWithFormat: @"unrecognized selector sent to instance %p", self] userInfo: nil];
    return;
}</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">Class</span> observedClass = object_getClass(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>);
NSString * className = NSStringFromClass(observedClass);

<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果被监听者没有LXDObserver_,那么判断是否需要创建新类</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (![className hasPrefix: kLXDkvoClassPrefix]) <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">{
    observedClass = [self createKVOClassWithOriginalClassName: className];
    object_setClass(self, observedClass);
}</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//重新实现setter方法,使其完成</span>
<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> char * types = method_getTypeEncoding(setterMethod);
class_addMethod(observedClass, setterSelector, (IMP)KVO_setter, types);</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>

在重新实现setter方法的时候,有两个重要的方法:willChangeValueForKey和didChangeValueForKey,分别在赋值前后进行调用。此外,还要遍历所有的回调监听者,然后通知这些监听者:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> KVO_setter(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>, SEL _cmd, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> newValue)
{
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * setterName = NSStringFromSelector(_cmd);
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> * getterName = getterForSetter(setterName);
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (!getterName) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@throw</span> [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSException</span> exceptionWithName: NSInvalidArgumentException reason: [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat: @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"unrecognized selector sent to instance %p"</span>, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>] userInfo: <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>];
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;
    }

    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span> oldValue = [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> valueForKey: getterName];
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">struct</span> objc_super superClass = {
        <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.receiver</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>,
        <span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.super_class</span> = class_getSuperclass(object_getClass(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>))
    };

    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> willChangeValueForKey: getterName];
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> (*objc_msgSendSuperKVO)(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> *, SEL, <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">id</span>) = (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> *)objc_msgSendSuper;
    objc_msgSendSuperKVO(&superClass, _cmd, newValue);
    [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> didChangeValueForKey: getterName];

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//获取所有监听回调对象进行回调</span>
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span> * observers = objc_getAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>, (__bridge <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">const</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> *)kLXDkvoAssiociateObserver);
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (LXD_ObserverInfo * info in observers) {
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> ([info<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.key</span> isEqualToString: getterName]) {        
            <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_async</span>(dispatch_queue_create(DISPATCH_QUEUE_PRIORITY_DEFAULT, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>), ^{
            info<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.handler</span>(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>, getterName, oldValue, newValue);
            });
        }
    }
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li></ul>

所有的监听者通过动态绑定的方式将其存储起来,但这样也会产生强引用,所以我们还需要提供释放监听的方法:

<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">- (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)LXD_removeObserver:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSObject</span> *)object forKey:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *)key
{
    <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span> * observers = objc_getAssociatedObject(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>, (__bridge <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span> *)kLXDkvoAssiociateObserver);

    LXD_ObserverInfo * observerRemoved = <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>;
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (LXD_ObserverInfo * observerInfo in observers) {

        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (observerInfo<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.observer</span> == object && [observerInfo<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.key</span> isEqualToString: key]) {

            observerRemoved = observerInfo;
            <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">break</span>;
        }
    }
    [observers removeObject: observerRemoved];
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li></ul>

虽然上面已经粗略的实现了kvo,并且我们还能自定义回调方式。使用target-action或者block的方式进行回调会比单一的系统回调要全面的多。但kvo真正的实现并没有这么简单,上述代码目前只能实现对象类型的监听,基本类型无法监听,况且还有keyPath可以监听对象的成员对象的属性这种更强大的功能。

尾言

对于基本类型的监听,苹果可能是通过void *类型对对象进行桥接转换,然后直接获取内存,通过type encoding我们可以获取所有setter对象的具体类型,虽然实现比较麻烦,但是确实能够达成类似的效果。 
钻研kvo的实现可以让我们对苹果的代码实现有更深层次的了解,这些知识涉及到了更深层次的技术,探究它们对我们的开发视野有着很重要的作用。同时,对比其他的回调方式,KVO的实现在创建子类、重写方法等等方面的内存消耗是很巨大的,因此博主更加推荐使用delegate、block等回调方式,甚至直接使用method-swizzling来替换这种重写setter方式也是可行的。 
ps:昨天有人问我说为什么kvo不直接通过重写setter方法的方式来进行回调,而要创建一个中间类。诚然,method_swizzling是一个很赞的机制,完全能用它来满足监听需求。但是,如果我们要监听的对象是tableView呢?正常而言,一款应用中远不止一个列表,使用method_swizzling会导致所有的列表都添加了监听回调,先不考虑这可能导致的崩溃风险,所有继承自tableView的视图(包括自身)的setter都受到了影响。而使用中间类却避免了这个问题

转自:http://www.jianshu.com/p/742b4b248da9

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/531535
推荐阅读
相关标签
  

闽ICP备14008679号