-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.json
1 lines (1 loc) · 28.5 KB
/
17.json
1
{code: 0, data:{"modifiedTime": "1444734000.0", "title": "iOS\u5f00\u53d1\u4e2dKVO\u7684\u5185\u90e8\u5b9e\u73b0", "createTime": "1414728780.0", "summary": "iOS\u5f00\u53d1\u4e2dKVO\u7684\u5185\u90e8\u5b9e\u73b0", "content": "<p><a href=\"http://blog.csdn.net/wzzvictory/article/details/9674431\" target=\"_blank\" rel=\"nofollow\">http://blog.csdn.net/wzzvictory/article/details/9674431</a></p>\n<p>KVO\u662f\u5b9e\u73b0Cocoa Bindings\u7684\u57fa\u7840\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6cd5\uff0c\u5f53\u67d0\u4e2a\u5c5e\u6027\u6539\u53d8\u65f6\uff0c\u76f8\u5e94\u7684objects\u4f1a\u88ab\u901a\u77e5\u5230\u3002\u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\uff0c\u8fd9\u79cd\u89c2\u5bdf\u8005\u6a21\u5f0f\u901a\u5e38\u9700\u8981\u5355\u72ec\u5b9e\u73b0\uff0c\u800c\u5728Objective-C\u4e2d\uff0c\u901a\u5e38\u65e0\u987b\u589e\u52a0\u989d\u5916\u4ee3\u7801\u5373\u53ef\u4f7f\u7528\u3002</p>\n<h4>\u6982\u89c8</h4>\n<p>\u8fd9\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u5462\uff1f\u5176\u5b9e\u8fd9\u90fd\u662f\u901a\u8fc7Objective-C\u5f3a\u5927\u7684\u8fd0\u884c\u65f6(runtime)\u5b9e\u73b0\u7684\u3002\u5f53\u4f60\u7b2c\u4e00\u6b21\u89c2\u5bdf\u67d0\u4e2aobject \u65f6\uff0cruntime\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ee7\u627f\u539f\u5148class\u7684subclass\u3002\u5728\u8fd9\u4e2a\u65b0\u7684class\u4e2d\uff0c\u5b83\u91cd\u5199\u4e86\u6240\u6709\u88ab\u89c2\u5bdf\u7684key\uff0c\u7136\u540e\u5c06object\u7684isa\u6307\u9488\u6307\u5411\u65b0\u521b\u5efa\u7684class\uff08\u8fd9\u4e2a\u6307\u9488\u544a\u8bc9Objective-C\u8fd0\u884c\u65f6\u67d0\u4e2aobject\u5230\u5e95\u662f\u54ea\u79cd\u7c7b\u578b\u7684object\uff09\u3002\u6240\u4ee5object\u795e\u5947\u5730\u53d8\u6210\u4e86\u65b0\u7684\u5b50\u7c7b\u7684\u5b9e\u4f8b\u3002</p>\n<p>\u8fd9\u4e9b\u88ab\u91cd\u5199\u7684\u65b9\u6cd5\u5b9e\u73b0\u4e86\u5982\u4f55\u901a\u77e5\u89c2\u5bdf\u8005\u4eec\u3002\u5f53\u6539\u53d8\u4e00\u4e2akey\u65f6\uff0c\u4f1a\u89e6\u53d1setKey\u65b9\u6cd5\uff0c\u4f46\u8fd9\u4e2a\u65b9\u6cd5\u88ab\u91cd\u5199\u4e86\uff0c\u5e76\u4e14\u5728\u5185\u90e8\u6dfb\u52a0\u4e86\u53d1\u9001\u901a\u77e5\u673a\u5236\u3002\uff08\u5f53\u7136\u4e5f\u53ef\u4ee5\u4e0d\u8d70setXXX\u65b9\u6cd5\uff0c\u6bd4\u5982\u76f4\u63a5\u4fee\u6539iVar\uff0c\u4f46\u4e0d\u63a8\u8350\u8fd9\u4e48\u505a\uff09\u3002</p>\n<p>\u6709\u610f\u601d\u7684\u662f\uff1a\u82f9\u679c\u4e0d\u5e0c\u671b\u8fd9\u4e2a\u673a\u5236\u66b4\u9732\u5728\u5916\u90e8\u3002\u9664\u4e86setters\uff0c\u8fd9\u4e2a\u52a8\u6001\u751f\u6210\u7684\u5b50\u7c7b\u540c\u65f6\u4e5f\u91cd\u5199\u4e86-class\u65b9\u6cd5\uff0c\u4f9d\u65e7\u8fd4\u56de\u539f\u5148\u7684class\uff01\u5982\u679c\u4e0d\u4ed4\u7ec6\u770b\u7684\u8bdd\uff0c\u88abKVO\u8fc7\u7684object\u770b\u8d77\u6765\u548c\u539f\u5148\u7684object\u6ca1\u4ec0\u4e48\u4e24\u6837\u3002</p>\n<h4>\u6df1\u5165\u63a2\u7a76</h4>\n<p>\u4e0b\u9762\u6765\u770b\u770b\u8fd9\u4e9b\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u3002\u6211\u5199\u4e86\u4e2a\u7a0b\u5e8f\u6765\u6f14\u793a\u9690\u85cf\u5728KVO\u80cc\u540e\u7684\u673a\u5236\u3002</p>\n<div class=\"highlight\"><pre><code><span class=\"c1\">// gcc -o kvoexplorer -framework Foundation kvoexplorer.m </span>\n<span class=\"cp\">#import <Foundation/Foundation.h> </span>\n<span class=\"cp\">#import <objc/runtime.h> </span>\n<span class=\"err\">@</span><span class=\"n\">interface</span> <span class=\"nl\">TestClass</span> <span class=\"p\">:</span> <span class=\"n\">NSObject</span> <span class=\"p\">{</span> \n <span class=\"kt\">int</span> <span class=\"n\">x</span><span class=\"p\">;</span> \n <span class=\"kt\">int</span> <span class=\"n\">y</span><span class=\"p\">;</span> \n <span class=\"kt\">int</span> <span class=\"n\">z</span><span class=\"p\">;</span> \n<span class=\"p\">}</span> \n\n<span class=\"err\">@</span><span class=\"n\">property</span> <span class=\"kt\">int</span> <span class=\"n\">x</span><span class=\"p\">;</span> \n<span class=\"err\">@</span><span class=\"n\">property</span> <span class=\"kt\">int</span> <span class=\"n\">y</span><span class=\"p\">;</span> \n<span class=\"err\">@</span><span class=\"n\">property</span> <span class=\"kt\">int</span> <span class=\"n\">z</span><span class=\"p\">;</span> \n\n<span class=\"err\">@</span><span class=\"n\">end</span> \n\n<span class=\"err\">@</span><span class=\"n\">implementation</span> <span class=\"n\">TestClass</span> \n\n<span class=\"err\">@</span><span class=\"n\">synthesize</span> <span class=\"n\">x</span><span class=\"p\">,</span> <span class=\"n\">y</span><span class=\"p\">,</span> <span class=\"n\">z</span><span class=\"p\">;</span> \n\n<span class=\"err\">@</span><span class=\"n\">end</span> \n\n<span class=\"k\">static</span> <span class=\"n\">NSArray</span> <span class=\"o\">*</span><span class=\"n\">ClassMethodNames</span><span class=\"p\">(</span><span class=\"n\">Class</span> <span class=\"n\">c</span><span class=\"p\">)</span> <span class=\"p\">{</span> \n <span class=\"n\">NSMutableArray</span> <span class=\"o\">*</span><span class=\"n\">array</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">NSMutableArray</span> <span class=\"n\">array</span><span class=\"p\">];</span> \n <span class=\"kt\">unsigned</span> <span class=\"kt\">int</span> <span class=\"n\">methodCount</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span> \n\n <span class=\"n\">Method</span> <span class=\"o\">*</span><span class=\"n\">methodList</span> <span class=\"o\">=</span> <span class=\"n\">class_copyMethodList</span><span class=\"p\">(</span><span class=\"n\">c</span><span class=\"p\">,</span> <span class=\"o\">&</span><span class=\"n\">methodCount</span><span class=\"p\">);</span> \n <span class=\"kt\">unsigned</span> <span class=\"kt\">int</span> <span class=\"n\">i</span><span class=\"p\">;</span> \n\n <span class=\"k\">for</span><span class=\"p\">(</span><span class=\"n\">i</span> <span class=\"o\">=</span> <span class=\"mi\">0</span><span class=\"p\">;</span> <span class=\"n\">i</span> <span class=\"o\"><</span> <span class=\"n\">methodCount</span><span class=\"p\">;</span> <span class=\"n\">i</span><span class=\"o\">++</span><span class=\"p\">)</span> \n <span class=\"p\">[</span><span class=\"n\">array</span> <span class=\"nl\">addObject</span><span class=\"p\">:</span> <span class=\"n\">NSStringFromSelector</span><span class=\"p\">(</span><span class=\"n\">method_getName</span><span class=\"p\">(</span><span class=\"n\">methodList</span><span class=\"p\">[</span><span class=\"n\">i</span><span class=\"p\">]))];</span> \n\n <span class=\"n\">free</span><span class=\"p\">(</span><span class=\"n\">methodList</span><span class=\"p\">);</span> \n\n <span class=\"k\">return</span> <span class=\"n\">array</span><span class=\"p\">;</span> \n<span class=\"p\">}</span> \n\n<span class=\"k\">static</span> <span class=\"kt\">void</span> <span class=\"n\">PrintDescription</span><span class=\"p\">(</span><span class=\"n\">NSString</span> <span class=\"o\">*</span><span class=\"n\">name</span><span class=\"p\">,</span> <span class=\"n\">id</span> <span class=\"n\">obj</span><span class=\"p\">)</span> <span class=\"p\">{</span> \n <span class=\"n\">NSString</span> <span class=\"o\">*</span><span class=\"n\">str</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">NSString</span> <span class=\"nl\">stringWithFormat</span><span class=\"p\">:</span><span class=\"err\">@</span><span class=\"s\">"%@: %@</span><span class=\"se\">\\n\\t</span><span class=\"s\">NSObject class %s</span><span class=\"se\">\\n\\t</span><span class=\"s\">libobjc class %s</span><span class=\"se\">\\n\\t</span><span class=\"s\">implements methods <%@>"</span><span class=\"p\">,</span> \n <span class=\"n\">name</span><span class=\"p\">,</span> <span class=\"n\">obj</span><span class=\"p\">,</span> <span class=\"n\">class_getName</span><span class=\"p\">([</span><span class=\"n\">obj</span> <span class=\"n\">class</span><span class=\"p\">]),</span>\n <span class=\"n\">class_getName</span><span class=\"p\">(</span><span class=\"n\">obj</span><span class=\"o\">-></span><span class=\"n\">isa</span><span class=\"p\">),</span>\n <span class=\"p\">[</span><span class=\"n\">ClassMethodNames</span><span class=\"p\">(</span><span class=\"n\">obj</span><span class=\"o\">-></span><span class=\"n\">isa</span><span class=\"p\">)</span> <span class=\"nl\">componentsJoinedByString</span><span class=\"p\">:</span><span class=\"err\">@</span><span class=\"s\">", "</span><span class=\"p\">]];</span> <span class=\"n\">printf</span><span class=\"p\">(</span><span class=\"s\">"%s</span><span class=\"se\">\\n</span><span class=\"s\">"</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"n\">str</span> <span class=\"n\">UTF8String</span><span class=\"p\">]);</span> \n<span class=\"p\">}</span> \n\n<span class=\"kt\">int</span> <span class=\"n\">main</span><span class=\"p\">(</span><span class=\"kt\">int</span> <span class=\"n\">argc</span><span class=\"p\">,</span> <span class=\"kt\">char</span> <span class=\"o\">**</span><span class=\"n\">argv</span><span class=\"p\">)</span> <span class=\"p\">{</span> \n <span class=\"p\">[</span><span class=\"n\">NSAutoreleasePool</span> <span class=\"n\">new</span><span class=\"p\">];</span> \n\n <span class=\"n\">TestClass</span> <span class=\"o\">*</span><span class=\"n\">x</span> <span class=\"o\">=</span> <span class=\"p\">[[</span><span class=\"n\">TestClass</span> <span class=\"n\">alloc</span><span class=\"p\">]</span> <span class=\"n\">init</span><span class=\"p\">];</span> \n <span class=\"n\">TestClass</span> <span class=\"o\">*</span><span class=\"n\">y</span> <span class=\"o\">=</span> <span class=\"p\">[[</span><span class=\"n\">TestClass</span> <span class=\"n\">alloc</span><span class=\"p\">]</span> <span class=\"n\">init</span><span class=\"p\">];</span> \n <span class=\"n\">TestClass</span> <span class=\"o\">*</span><span class=\"n\">xy</span> <span class=\"o\">=</span> <span class=\"p\">[[</span><span class=\"n\">TestClass</span> <span class=\"n\">alloc</span><span class=\"p\">]</span> <span class=\"n\">init</span><span class=\"p\">];</span> \n <span class=\"n\">TestClass</span> <span class=\"o\">*</span><span class=\"n\">control</span> <span class=\"o\">=</span> <span class=\"p\">[[</span><span class=\"n\">TestClass</span> <span class=\"n\">alloc</span><span class=\"p\">]</span> <span class=\"n\">init</span><span class=\"p\">];</span> \n\n <span class=\"p\">[</span><span class=\"n\">x</span> <span class=\"nl\">addObserver</span><span class=\"p\">:</span><span class=\"n\">x</span> <span class=\"nl\">forKeyPath</span><span class=\"p\">:</span><span class=\"err\">@</span><span class=\"s\">"x"</span> <span class=\"nl\">options</span><span class=\"p\">:</span><span class=\"mi\">0</span> <span class=\"nl\">context</span><span class=\"p\">:</span><span class=\"nb\">NULL</span><span class=\"p\">];</span> \n <span class=\"p\">[</span><span class=\"n\">xy</span> <span class=\"nl\">addObserver</span><span class=\"p\">:</span><span class=\"n\">xy</span> <span class=\"nl\">forKeyPath</span><span class=\"p\">:</span><span class=\"err\">@</span><span class=\"s\">"x"</span> <span class=\"nl\">options</span><span class=\"p\">:</span><span class=\"mi\">0</span> <span class=\"nl\">context</span><span class=\"p\">:</span><span class=\"nb\">NULL</span><span class=\"p\">];</span> \n <span class=\"p\">[</span><span class=\"n\">y</span> <span class=\"nl\">addObserver</span><span class=\"p\">:</span><span class=\"n\">y</span> <span class=\"nl\">forKeyPath</span><span class=\"p\">:</span><span class=\"err\">@</span><span class=\"s\">"y"</span> <span class=\"nl\">options</span><span class=\"p\">:</span><span class=\"mi\">0</span> <span class=\"nl\">context</span><span class=\"p\">:</span><span class=\"nb\">NULL</span><span class=\"p\">];</span> \n <span class=\"p\">[</span><span class=\"n\">xy</span> <span class=\"nl\">addObserver</span><span class=\"p\">:</span><span class=\"n\">xy</span> <span class=\"nl\">forKeyPath</span><span class=\"p\">:</span><span class=\"err\">@</span><span class=\"s\">"y"</span> <span class=\"nl\">options</span><span class=\"p\">:</span><span class=\"mi\">0</span> <span class=\"nl\">context</span><span class=\"p\">:</span><span class=\"nb\">NULL</span><span class=\"p\">];</span> \n <span class=\"n\">PrintDescription</span><span class=\"p\">(</span><span class=\"err\">@</span><span class=\"s\">"control"</span><span class=\"p\">,</span> <span class=\"n\">control</span><span class=\"p\">);</span> \n <span class=\"n\">PrintDescription</span><span class=\"p\">(</span><span class=\"err\">@</span><span class=\"s\">"x"</span><span class=\"p\">,</span> <span class=\"n\">x</span><span class=\"p\">);</span> \n <span class=\"n\">PrintDescription</span><span class=\"p\">(</span><span class=\"err\">@</span><span class=\"s\">"y"</span><span class=\"p\">,</span> <span class=\"n\">y</span><span class=\"p\">);</span> \n <span class=\"n\">PrintDescription</span><span class=\"p\">(</span><span class=\"err\">@</span><span class=\"s\">"xy"</span><span class=\"p\">,</span> <span class=\"n\">xy</span><span class=\"p\">);</span> \n\n <span class=\"n\">printf</span><span class=\"p\">(</span><span class=\"s\">"Using NSObject methods, normal setX: is %p, overridden setX: is %p</span><span class=\"se\">\\n</span><span class=\"s\">"</span><span class=\"p\">,</span> <span class=\"p\">[</span><span class=\"n\">control</span> <span class=\"nl\">methodForSelector</span><span class=\"p\">:</span><span class=\"err\">@</span><span class=\"n\">selector</span><span class=\"p\">(</span><span class=\"nl\">setX</span><span class=\"p\">:)],</span> \n <span class=\"p\">[</span><span class=\"n\">x</span> <span class=\"nl\">methodForSelector</span><span class=\"p\">:</span><span class=\"err\">@</span><span class=\"n\">selector</span><span class=\"p\">(</span><span class=\"nl\">setX</span><span class=\"p\">:)]);</span> \n <span class=\"n\">printf</span><span class=\"p\">(</span><span class=\"s\">"Using libobjc functions, normal setX: is %p, overridden setX: is %p</span><span class=\"se\">\\n</span><span class=\"s\">"</span><span class=\"p\">,</span> <span class=\"n\">method_getImplementation</span><span class=\"p\">(</span><span class=\"n\">class_getInstanceMethod</span><span class=\"p\">(</span><span class=\"n\">object_getClass</span><span class=\"p\">(</span><span class=\"n\">control</span><span class=\"p\">),</span> <span class=\"err\">@</span><span class=\"n\">selector</span><span class=\"p\">(</span><span class=\"nl\">setX</span><span class=\"p\">:))),</span> <span class=\"n\">method_getImplementation</span><span class=\"p\">(</span><span class=\"n\">class_getInstanceMethod</span><span class=\"p\">(</span><span class=\"n\">object_getClass</span><span class=\"p\">(</span><span class=\"n\">x</span><span class=\"p\">),</span> <span class=\"err\">@</span><span class=\"n\">selector</span><span class=\"p\">(</span><span class=\"nl\">setX</span><span class=\"p\">:))));</span> \n\n <span class=\"k\">return</span> <span class=\"mi\">0</span><span class=\"p\">;</span> \n<span class=\"p\">}</span>\n</code></pre></div>\n<p>\u6211\u4eec\u4ece\u5934\u5230\u5c3e\u7ec6\u7ec6\u770b\u6765\u3002</p>\n<p>\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2aTestClass\u7684\u7c7b\uff0c\u5b83\u67093\u4e2a\u5c5e\u6027\u3002</p>\n<p>\u7136\u540e\u5b9a\u4e49\u4e86\u4e00\u4e9b\u65b9\u4fbf\u8c03\u8bd5\u7684\u65b9\u6cd5\u3002ClassMethodNames\u4f7f\u7528Objective-C\u8fd0\u884c\u65f6\u65b9\u6cd5\u6765\u904d\u5386\u4e00\u4e2aclass\uff0c\u5f97\u5230\u65b9\u6cd5\u5217\u8868\u3002\u6ce8\u610f\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u5305\u62ec\u7236\u7c7b\u7684\u65b9\u6cd5\u3002PrintDescription\u6253\u5370object\u7684\u6240\u6709\u4fe1\u606f\uff0c\u5305\u62ecclass\u4fe1\u606f\uff08\u5305\u62ec-class\u548c\u901a\u8fc7\u8fd0\u884c\u65f6\u5f97\u5230\u7684class\uff09\uff0c\u4ee5\u53ca\u8fd9\u4e2aclass\u5b9e\u73b0\u7684\u65b9\u6cd5\u3002</p>\n<p>\u7136\u540e\u521b\u5efa\u4e864\u4e2aTestClass\u5b9e\u4f8b\uff0c\u6bcf\u4e00\u4e2a\u90fd\u4f7f\u7528\u4e86\u4e0d\u540c\u7684\u89c2\u5bdf\u65b9\u5f0f\u3002x\u5b9e\u4f8b\u6709\u4e00\u4e2a\u89c2\u5bdf\u8005\u89c2\u5bdfxkey\uff0cy, xy\u4e5f\u7c7b\u4f3c\u3002\u4e3a\u4e86\u505a\u6bd4\u8f83\uff0czkey\u6ca1\u6709\u89c2\u5bdf\u8005\u3002\u6700\u540econtrol\u5b9e\u4f8b\u6ca1\u6709\u4efb\u4f55\u89c2\u5bdf\u8005\u3002</p>\n<p>\u7136\u540e\u6253\u5370\u51fa4\u4e2aobjects\u7684description\u3002</p>\n<p>\u4e4b\u540e\u7ee7\u7eed\u6253\u5370\u88ab\u91cd\u5199\u7684setter\u5185\u5b58\u5730\u5740\uff0c\u4ee5\u53ca\u672a\u88ab\u91cd\u5199\u7684setter\u7684\u5185\u5b58\u5730\u5740\u505a\u6bd4\u8f83\u3002\u8fd9\u91cc\u505a\u4e86\u4e24\u6b21\uff0c\u662f\u56e0\u4e3a-methodForSelector:\u6ca1\u80fd\u5f97\u5230\u91cd\u5199\u7684\u65b9\u6cd5\u3002KVO\u8bd5\u56fe\u63a9\u76d6\u5b83\u5b9e\u9645\u4e0a\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684subclass\u8fd9\u4e2a\u4e8b\u5b9e\uff01\u4f46\u662f\u4f7f\u7528\u8fd0\u884c\u65f6\u7684\u65b9\u6cd5\u5c31\u539f\u5f62\u6bd5\u9732\u4e86\u3002</p>\n<p>\u8fd0\u884c\u4ee3\u7801</p>\n<p>\u770b\u770b\u8fd9\u6bb5\u4ee3\u7801\u7684\u8f93\u51fa</p>\n<div class=\"highlight\"><pre><code><span class=\"nl\">control</span><span class=\"p\">:</span> <span class=\"o\"><</span><span class=\"nl\">TestClass</span><span class=\"p\">:</span> <span class=\"mh\">0x104b20</span><span class=\"o\">></span> \n<span class=\"n\">NSObject</span> <span class=\"n\">class</span> <span class=\"n\">TestClass</span> \n<span class=\"n\">libobjc</span> <span class=\"n\">class</span> <span class=\"n\">TestClass</span> \n<span class=\"n\">implements</span> <span class=\"n\">methods</span> <span class=\"o\"><</span><span class=\"nl\">setX</span><span class=\"p\">:,</span> <span class=\"n\">x</span><span class=\"p\">,</span> <span class=\"nl\">setY</span><span class=\"p\">:,</span> <span class=\"n\">y</span><span class=\"p\">,</span> <span class=\"nl\">setZ</span><span class=\"p\">:,</span> <span class=\"n\">z</span><span class=\"o\">></span> <span class=\"nl\">x</span><span class=\"p\">:</span> <span class=\"o\"><</span><span class=\"nl\">TestClass</span><span class=\"p\">:</span> <span class=\"mh\">0x103280</span><span class=\"o\">></span> \n<span class=\"n\">NSObject</span> <span class=\"n\">class</span> <span class=\"n\">TestClass</span> \n<span class=\"n\">libobjc</span> <span class=\"n\">class</span> <span class=\"n\">NSKVONotifying_TestClass</span> \n<span class=\"n\">implements</span> <span class=\"n\">methods</span> <span class=\"o\"><</span><span class=\"nl\">setY</span><span class=\"p\">:,</span> <span class=\"nl\">setX</span><span class=\"p\">:,</span> <span class=\"n\">class</span><span class=\"p\">,</span> <span class=\"n\">dealloc</span><span class=\"p\">,</span> <span class=\"n\">_isKVOA</span><span class=\"o\">></span> <span class=\"nl\">y</span><span class=\"p\">:</span> <span class=\"o\"><</span><span class=\"nl\">TestClass</span><span class=\"p\">:</span> <span class=\"mh\">0x104b00</span><span class=\"o\">></span> <span class=\"n\">NSObject</span> <span class=\"n\">class</span> <span class=\"n\">TestClass</span> \n<span class=\"n\">libobjc</span> <span class=\"n\">class</span> <span class=\"n\">NSKVONotifying_TestClass</span> \n<span class=\"n\">implements</span> <span class=\"n\">methods</span> <span class=\"o\"><</span><span class=\"nl\">setY</span><span class=\"p\">:,</span> <span class=\"nl\">setX</span><span class=\"p\">:,</span> <span class=\"n\">class</span><span class=\"p\">,</span> <span class=\"n\">dealloc</span><span class=\"p\">,</span> <span class=\"n\">_isKVOA</span><span class=\"o\">></span> <span class=\"nl\">xy</span><span class=\"p\">:</span> <span class=\"o\"><</span><span class=\"nl\">TestClass</span><span class=\"p\">:</span> <span class=\"mh\">0x104b10</span><span class=\"o\">></span> <span class=\"n\">NSObject</span> <span class=\"n\">class</span> <span class=\"n\">TestClass</span> \n<span class=\"n\">libobjc</span> <span class=\"n\">class</span> <span class=\"n\">NSKVONotifying_TestClass</span> \n<span class=\"n\">implements</span> <span class=\"n\">methods</span> <span class=\"o\"><</span><span class=\"nl\">setY</span><span class=\"p\">:,</span> <span class=\"nl\">setX</span><span class=\"p\">:,</span> <span class=\"n\">class</span><span class=\"p\">,</span> <span class=\"n\">dealloc</span><span class=\"p\">,</span> <span class=\"n\">_isKVOA</span><span class=\"o\">></span> <span class=\"n\">Using</span> <span class=\"n\">NSObject</span> <span class=\"n\">methods</span><span class=\"p\">,</span> <span class=\"n\">normal</span> <span class=\"nl\">setX</span><span class=\"p\">:</span> <span class=\"n\">is</span> <span class=\"mh\">0x195e</span><span class=\"p\">,</span> <span class=\"n\">overridden</span> <span class=\"nl\">setX</span><span class=\"p\">:</span> <span class=\"n\">is</span> <span class=\"mh\">0x195e</span> <span class=\"n\">Using</span> <span class=\"n\">libobjc</span> <span class=\"n\">functions</span><span class=\"p\">,</span> <span class=\"n\">normal</span> <span class=\"nl\">setX</span><span class=\"p\">:</span> <span class=\"n\">is</span> <span class=\"mh\">0x195e</span><span class=\"p\">,</span> <span class=\"n\">overridden</span> <span class=\"nl\">setX</span><span class=\"p\">:</span> <span class=\"n\">is</span> <span class=\"mh\">0x96a1a550</span>\n</code></pre></div>\n<p>\u9996\u5148\uff0c\u5b83\u8f93\u51fa\u4e86controlobject\uff0c\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\uff0c\u5b83\u7684class\u662fTestClass\uff0c\u5e76\u4e14\u5b9e\u73b0\u4e866\u4e2aset/get\u65b9\u6cd5\u3002</p>\n<p>\u7136\u540e\u662f3\u4e2a\u88ab\u89c2\u5bdf\u7684objects\u3002\u6ce8\u610f-class\u4ecd\u7136\u663e\u793a\u7684\u662fTestClass\uff0c\u4f7f\u7528object_getClass\u663e\u793a\u4e86\u8fd9\u4e2aobject\u7684\u771f\u9762\u76ee\uff1a\u5b83\u662fNSKVONotifying_TestClass\u7684\u4e00\u4e2a\u5b9e\u4f8b\u3002\u8fd9\u4e2aNSKVONotifying_TestClass\u5c31\u662f\u52a8\u6001\u751f\u6210\u7684subclass\uff01</p>\n<p>\u6ce8\u610f\uff0c\u5b83\u662f\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e24\u4e2a\u88ab\u89c2\u5bdf\u7684setters\u7684\u3002\u4f60\u4f1a\u53d1\u73b0\uff0c\u5b83\u5f88\u806a\u660e\uff0c\u6ca1\u6709\u91cd\u5199-setZ:\uff0c\u867d\u7136\u5b83\u4e5f\u662f\u4e2a setter\uff0c\u56e0\u4e3a\u5b83\u6ca1\u6709\u88ab\u89c2\u5bdf\u3002\u540c\u65f6\u6ce8\u610f\u5230\uff0c3\u4e2a\u5b9e\u4f8b\u5bf9\u5e94\u7684\u662f\u540c\u4e00\u4e2aclass\uff0c\u4e5f\u5c31\u662f\u8bf4\u4e24\u4e2asetters\u90fd\u88ab\u91cd\u5199\u4e86\uff0c\u5c3d\u7ba1\u5176\u4e2d\u7684\u4e24\u4e2a\u5b9e\u4f8b\u53ea\u89c2\u5bdf\u4e86\u4e00 \u4e2a\u5c5e\u6027\u3002\u8fd9\u4f1a\u5e26\u6765\u4e00\u70b9\u6548\u7387\u4e0a\u7684\u95ee\u9898\uff0c\u56e0\u4e3a\u5373\u4f7f\u6ca1\u6709\u88ab\u89c2\u5bdf\u7684property\u4e5f\u4f1a\u8d70\u88ab\u91cd\u5199\u7684setter\uff0c\u4f46\u82f9\u679c\u663e\u7136\u89c9\u5f97\u8fd9\u6bd4\u5206\u5f00\u751f\u6210\u52a8\u6001\u7684 subclass\u66f4\u597d\uff0c\u6211\u4e5f\u89c9\u5f97\u8fd9\u662f\u4e2a\u6b63\u786e\u7684\u9009\u62e9\u3002</p>\n<p>\u4f60\u4f1a\u770b\u52303\u4e2a\u5176\u4ed6\u7684\u65b9\u6cd5\u3002\u6709\u4e4b\u524d\u63d0\u5230\u8fc7\u7684\u88ab\u91cd\u5199\u7684-class\u65b9\u6cd5\uff0c\u5047\u88c5\u81ea\u5df1\u8fd8\u662f\u539f\u6765\u7684class\u3002\u8fd8\u6709-dealloc\u65b9\u6cd5\u5904\u7406\u4e00\u4e9b\u6536\u5c3e\u5de5\u4f5c\u3002\u8fd8\u6709\u4e00\u4e2a_isKVOA\u65b9\u6cd5\uff0c\u770b\u8d77\u6765\u50cf\u662f\u4e00\u4e2a\u79c1\u6709\u65b9\u6cd5\u3002</p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8f93\u51fa-setX:\u7684\u5b9e\u73b0\u3002\u4f7f\u7528-methodForSelector:\u8fd4\u56de\u7684\u662f\u76f8\u540c\u7684\u503c\u3002\u56e0\u4e3a-setX:\u5df2\u7ecf\u5728\u5b50\u7c7b\u88ab\u91cd\u5199\u4e86\uff0c\u8fd9\u4e5f\u5c31\u610f\u5473\u7740methodForSelector:\u5728\u5185\u90e8\u5b9e\u73b0\u4e2d\u4f7f\u7528\u4e86-class\uff0c\u4e8e\u662f\u5f97\u5230\u4e86\u9519\u8bef\u7684\u7ed3\u679c\u3002</p>\n<p>\u6700\u540e\u6211\u4eec\u901a\u8fc7\u8fd0\u884c\u65f6\u5f97\u5230\u4e86\u4e0d\u540c\u7684\u8f93\u51fa\u7ed3\u679c\u3002</p>\n<p>\u4f5c\u4e3a\u4e00\u4e2a\u4f18\u79c0\u7684\u63a2\u7d22\u8005\uff0c\u6211\u4eec\u8fdb\u5165debugger\u6765\u770b\u770b\u8fd9\u7b2c\u4e8c\u4e2a\u65b9\u6cd5\u7684\u5b9e\u73b0\u5230\u5e95\u662f\u600e\u6837\u7684\uff1a</p>\n<p>(gdb) print (IMP)0x96a1a550 $1 = (IMP) 0x96a1a550 <_NSSetIntValueAndNotify>\n\u770b\u8d77\u6765\u662f\u4e00\u4e2a\u5185\u90e8\u65b9\u6cd5\uff0c\u5bf9Foundation\u4f7f\u7528nm -a\u5f97\u5230\u4e00\u4e2a\u5b8c\u6574\u7684\u79c1\u6709\u65b9\u6cd5\u5217\u8868\uff1a</p>\n<div class=\"highlight\"><pre><code><span class=\"mo\">0013</span><span class=\"n\">df80</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetBoolValueAndNotify</span> <span class=\"mo\">000</span><span class=\"n\">a0480</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetCharValueAndNotify</span>\n<span class=\"mf\">0013e120</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetDoubleValueAndNotify</span> <span class=\"mf\">0013e1</span><span class=\"n\">f0</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetFloatValueAndNotify</span> \n<span class=\"mf\">000e3550</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetIntValueAndNotify</span> <span class=\"mf\">0013e390</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetLongLongValueAndNotify</span> \n<span class=\"mf\">0013e2</span><span class=\"n\">c0</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetLongValueAndNotify</span> <span class=\"mo\">000</span><span class=\"mi\">89</span><span class=\"n\">df0</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetObjectValueAndNotify</span> \n<span class=\"mf\">0013e6</span><span class=\"n\">f0</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetPointValueAndNotify</span> <span class=\"mf\">0013e7</span><span class=\"n\">d0</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetRangeValueAndNotify</span> \n<span class=\"mf\">0013e8</span><span class=\"n\">b0</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetRectValueAndNotify</span> <span class=\"mf\">0013e550</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetShortValueAndNotify</span> \n<span class=\"mo\">000</span><span class=\"mi\">8</span><span class=\"n\">ab20</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetSizeValueAndNotify</span> <span class=\"mf\">0013e050</span> <span class=\"n\">t</span> \n<span class=\"n\">__NSSetUnsignedCharValueAndNotify</span> <span class=\"mf\">0009f</span><span class=\"n\">cd0</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetUnsignedIntValueAndNotify</span> \n<span class=\"mf\">0013e470</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetUnsignedLongLongValueAndNotify</span> <span class=\"mf\">0009f</span><span class=\"n\">c00</span> <span class=\"n\">t</span> \n<span class=\"n\">__NSSetUnsignedLongValueAndNotify</span> <span class=\"mf\">0013e620</span> <span class=\"n\">t</span> <span class=\"n\">__NSSetUnsignedShortValueAndNotify</span>\n</code></pre></div>\n<p>\u8fd9\u4e2a\u5217\u8868\u4e5f\u80fd\u53d1\u73b0\u4e00\u4e9b\u6709\u8da3\u7684\u4e1c\u897f\u3002\u6bd4\u5982\u82f9\u679c\u4e3a\u6bcf\u4e00\u79cdprimitive type\u90fd\u5199\u4e86\u5bf9\u5e94\u7684\u5b9e\u73b0\u3002Objective-C\u7684object\u4f1a\u7528\u5230\u7684\u5176\u5b9e\u53ea\u6709__NSSetObjectValueAndNotify\uff0c\u4f46\u9700\u8981\u4e00\u6574\u5957\u6765\u5bf9\u5e94\u5269\u4e0b\u7684\uff0c\u800c\u4e14\u770b\u8d77\u6765\u4e5f\u6ca1\u6709\u5b9e\u73b0\u5b8c\u5168\uff0c\u6bd4\u5982long dobule\u6216_Bool\u90fd\u6ca1\u6709\u3002\u751a\u81f3\u6ca1\u6709\u4e3a\u901a\u7528\u6307\u9488\u7c7b\u578b(generic pointer type)\u63d0\u4f9b\u65b9\u6cd5\u3002\u6240\u4ee5\uff0c\u4e0d\u5728\u8fd9\u4e2a\u65b9\u6cd5\u5217\u8868\u91cc\u7684\u5c5e\u6027\u5176\u5b9e\u662f\u4e0d\u652f\u6301KVO\u7684\u3002</p>\n<p>KVO\u662f\u4e00\u4e2a\u5f88\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u6709\u65f6\u5019\u8fc7\u4e8e\u5f3a\u5927\u4e86\uff0c\u5c24\u5176\u662f\u6709\u4e86\u81ea\u52a8\u89e6\u53d1\u901a\u77e5\u673a\u5236\u3002\u73b0\u5728\u4f60\u77e5\u9053\u5b83\u5185\u90e8\u662f\u600e\u4e48\u5b9e\u73b0\u7684\u4e86\uff0c\u8fd9\u4e9b\u77e5\u8bc6\u6216\u8bb8\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u4f7f\u7528\u5b83\uff0c\u6216\u5728\u5b83\u51fa\u9519\u65f6\u66f4\u65b9\u4fbf\u8c03\u8bd5\u3002</p>\n<p>\u5982\u679c\u4f60\u6253\u7b97\u4f7f\u7528KVO\uff0c\u6216\u8bb8\u53ef\u4ee5\u770b\u4e00\u4e0b\u6211\u7684\u53e6\u4e00\u7bc7\u6587\u7ae0Key-Value Observing Done Right</p>\n", "id": 17}}