{"id":42411,"date":"2023-12-11T14:56:30","date_gmt":"2023-12-11T06:56:30","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=42411"},"modified":"2023-12-11T14:56:30","modified_gmt":"2023-12-11T06:56:30","slug":"kvc-kvo%e7%9a%84%e5%ba%95%e5%b1%82%e5%8e%9f%e7%90%86%e5%92%8c%e4%bd%bf%e7%94%a8%e5%9c%ba%e6%99%af","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/12\/11\/kvc-kvo%e7%9a%84%e5%ba%95%e5%b1%82%e5%8e%9f%e7%90%86%e5%92%8c%e4%bd%bf%e7%94%a8%e5%9c%ba%e6%99%af\/","title":{"rendered":"KVC \/KVO\u7684\u5e95\u5c42\u539f\u7406\u548c\u4f7f\u7528\u573a\u666f"},"content":{"rendered":"<p>&#8220;`&#8221;<\/p>\n<p>&lt;h2&gt;1 KVC(KeyValueCoding)&lt;\/h2&gt;<\/p>\n<p>&lt;h3&gt;1.1 KVC \u5e38\u7528\u7684\u65b9\u6cd5&lt;\/h3&gt;<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-objc&quot;&quot; lang=&quot;&quot;objc&quot;&quot;&gt;(1)\u8d4b\u503c\u7c7b\u65b9\u6cd5<br \/>\n&#8211; (void)setValue:(nullable id)value forKey:(NSString *)key;<br \/>\n&#8211; (void)setValue:(nullable id)value forKeyPath:(NSString *)keyPath;<br \/>\n&#8211; (void)setValue:(nullable id)value forUndefinedKey:(NSString *)key;<br \/>\n&#8211; (void)setValuesForKeysWithDictionary:(NSDictionary&lt;NSString *, id&gt; *)keyedValues;<\/p>\n<p>(2)\u53d6\u503c\u7c7b\u65b9\u6cd5<br \/>\n\/\/ \u80fd\u53d6\u5f97\u79c1\u6709\u6210\u5458\u53d8\u91cf\u7684\u503c<br \/>\n&#8211; (id)valueForKey:(NSString *)key;<br \/>\n&#8211; (id)valueForKeyPath:(NSString *)keyPath;<br \/>\n&#8211; (NSDictionary *)dictionaryWithValuesForKeys:(NSArray *)keys;<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;h3&gt;1.2 KVC \u5e95\u5c42\u5b9e\u73b0\u539f\u7406&lt;\/h3&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-objc&quot;&quot; lang=&quot;&quot;objc&quot;&quot;&gt;\u5f53\u4e00\u4e2a\u5bf9\u8c61\u8c03\u7528setValue:forKey: \u65b9\u6cd5\u65f6,\u65b9\u6cd5\u5185\u90e8\u4f1a\u505a\u4ee5\u4e0b\u64cd\u4f5c:<br \/>\n 1.\u5224\u65ad\u6709\u6ca1\u6709\u6307\u5b9akey\u7684set\u65b9\u6cd5,\u5982\u679c\u6709set\u65b9\u6cd5,\u5c31\u4f1a\u8c03\u7528set\u65b9\u6cd5,\u7ed9\u8be5\u5c5e\u6027\u8d4b\u503c<br \/>\n 2.\u5982\u679c\u6ca1\u6709set\u65b9\u6cd5,\u5224\u65ad\u6709\u6ca1\u6709\u8ddfkey\u503c\u76f8\u540c\u4e14\u5e26\u6709\u4e0b\u5212\u7ebf\u7684\u6210\u5458\u5c5e\u6027(_key).\u5982\u679c\u6709,\u76f4\u63a5\u7ed9\u8be5\u6210\u5458\u5c5e\u6027\u8fdb\u884c\u8d4b\u503c<br \/>\n 3.\u5982\u679c\u6ca1\u6709\u6210\u5458\u5c5e\u6027_key,\u5224\u65ad\u6709\u6ca1\u6709\u8ddfkey\u76f8\u540c\u540d\u79f0\u7684\u5c5e\u6027.\u5982\u679c\u6709,\u76f4\u63a5\u7ed9\u8be5\u5c5e\u6027\u8fdb\u884c\u8d4b\u503c<br \/>\n 4.\u5982\u679c\u90fd\u6ca1\u6709,\u5c31\u4f1a\u8c03\u7528 valueforUndefinedKey \u548csetValue:forUndefinedKey:\u65b9\u6cd5<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;h3&gt;1.3 KVC \u7684\u4f7f\u7528\u573a\u666f&lt;\/h3&gt;<\/p>\n<p>&lt;h4&gt;1.3.1 \u8d4b\u503c&lt;\/h4&gt;<\/p>\n<p>(1) KVC \u7b80\u5355\u5c5e\u6027\u8d4b\u503c<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-objc&quot;&quot; lang=&quot;&quot;objc&quot;&quot;&gt;Person *p = [[Person alloc] init];<br \/>\n\/\/    p.name = @&quot;&quot;jack&quot;&quot;;<br \/>\n\/\/    p.money = 22.2;<br \/>\n\u4f7f\u7528setValue: forKey:\u65b9\u6cd5\u80fd\u591f\u7ed9\u5c5e\u6027\u8d4b\u503c,\u7b49\u4ef7\u4e8e\u76f4\u63a5\u7ed9\u5c5e\u6027\u8d4b\u503c<br \/>\n[p setValue:@&quot;&quot;rose&quot;&quot; forKey:@&quot;&quot;name&quot;&quot;];<br \/>\n[p setValue:@&quot;&quot;22.2&quot;&quot; forKey:@&quot;&quot;money&quot;&quot;];<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>(2) KVC\u590d\u6742\u5c5e\u6027\u8d4b\u503c<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-objc&quot;&quot; lang=&quot;&quot;objc&quot;&quot;&gt;\/\/\u7ed9Person\u6dfb\u52a0 Dog\u5c5e\u6027<br \/>\n   Person *p = [[Person alloc] init];<br \/>\n   p.dog = [[Dog alloc] init];<br \/>\n  \/\/ p.dog.name = @&quot;&quot;\u963f\u9ec4&quot;&quot;;<\/p>\n<p>1)setValue: forKeyPath: \u65b9\u6cd5\u7684\u4f7f\u7528<br \/>\n  \/\/\u4fee\u6539p.dog \u7684name \u5c5e\u6027<br \/>\n    [p.dog setValue:@&quot;&quot;wangcai&quot;&quot; forKeyPath:@&quot;&quot;name&quot;&quot;];<br \/>\n    [p setValue:@&quot;&quot;\u963f\u82b1&quot;&quot; forKeyPath:@&quot;&quot;dog.name&quot;&quot;];<\/p>\n<p>2)setValue: forKey: \u9519\u8bef\u7528\u6cd5<br \/>\n    [p setValue:@&quot;&quot;\u963f\u82b1&quot;&quot; forKey:@&quot;&quot;dog.name&quot;&quot;];<br \/>\n    NSLog(@&quot;&quot;%@&quot;&quot;, p.dog.name);<\/p>\n<p>3)\u76f4\u63a5\u4fee\u6539\u79c1\u6709\u6210\u5458\u53d8\u91cf<br \/>\n[p setValue:@&quot;&quot;\u65fa\u8d22&quot;&quot; forKeyPath:@&quot;&quot;_name&quot;&quot;];<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>(3) \u6dfb\u52a0\u79c1\u6709\u6210\u5458\u53d8\u91cf<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-objc&quot;&quot; lang=&quot;&quot;objc&quot;&quot;&gt;Person \u7c7b\u4e2d\u6dfb\u52a0\u79c1\u6709\u6210\u5458\u53d8\u91cf_age<br \/>\n[p setValue:@&quot;&quot;22&quot;&quot; forKeyPath:@&quot;&quot;_age&quot;&quot;];<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;h4&gt;1.3.2 \u5b57\u5178\u8f6c\u6a21\u578b&lt;\/h4&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-objc&quot;&quot; lang=&quot;&quot;objc&quot;&quot;&gt;(1)\u7b80\u5355\u7684\u5b57\u5178\u8f6c\u6a21\u578b<br \/>\n +(instancetype)videoWithDict:(NSDictionary *)dict<br \/>\n{<br \/>\n    JLVideo *videItem = [[JLVideo alloc] init];<br \/>\n    \/\/\u4ee5\u524d<br \/>\n\/\/    videItem.name = dict[@&quot;&quot;name&quot;&quot;];<br \/>\n\/\/    videItem.money = [dict[@&quot;&quot;money&quot;&quot;] doubleValue] ;<\/p>\n<p>    \/\/KVC,\u4f7f\u7528setValuesForKeysWithDictionary:\u65b9\u6cd5,\u8be5\u65b9\u6cd5\u9ed8\u8ba4\u6839\u636e\u5b57\u5178\u4e2d\u6bcf\u4e2a\u952e\u503c\u5bf9,\u8c03\u7528setValue:forKey\u65b9\u6cd5<br \/>\n    \/\/ \u7f3a\u70b9:\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u5fc5\u987b\u4e0e\u6a21\u578b\u4e2d\u7684\u952e\u503c\u5bf9\u5b8c\u5168\u5bf9\u5e94,\u5426\u5219\u7a0b\u5e8f\u4f1a\u5d29\u6e83<br \/>\n    [videItem setValuesForKeysWithDictionary:dict];<br \/>\n    return videItem;<br \/>\n}<\/p>\n<p>(2)\u590d\u6742\u7684\u5b57\u5178\u8f6c\u6a21\u578b<br \/>\n\u6ce8\u610f:\u590d\u6742\u5b57\u5178\u8f6c\u6a21\u578b\u4e0d\u80fd\u76f4\u63a5\u901a\u8fc7KVC \u8d4b\u503c,KVC\u53ea\u80fd\u5728\u7b80\u5355\u5b57\u5178\u4e2d\u4f7f\u7528,\u6bd4\u5982:<br \/>\n    NSDictionary *dict = @{<br \/>\n                       @&quot;&quot;name&quot;&quot; : @&quot;&quot;jack&quot;&quot;,<br \/>\n                       @&quot;&quot;money&quot;&quot;: @&quot;&quot;22.2&quot;&quot;,<br \/>\n                       @&quot;&quot;dog&quot;&quot; : @{<br \/>\n                               @&quot;&quot;name&quot;&quot; : @&quot;&quot;wangcai&quot;&quot;,<br \/>\n                               @&quot;&quot;money&quot;&quot;: @&quot;&quot;11.1&quot;&quot;,<\/p>\n<p>                               }<\/p>\n<p>                       };<br \/>\n   JLPerson *p = [[JLPerson alloc]init]; \/\/ p\u662f\u4e00\u4e2a\u6a21\u578b\u5bf9\u8c61<br \/>\n   [p setValuesForKeysWithDictionary:dict];<br \/>\n\u5185\u90e8\u8f6c\u6362\u539f\u7406:<br \/>\n\/\/    [p setValue:@&quot;&quot;jack&quot;&quot; forKey:@&quot;&quot;name&quot;&quot;];<br \/>\n\/\/    [p setValue:@&quot;&quot;22.2&quot;&quot; forKey:@&quot;&quot;money&quot;&quot;];<br \/>\n\/\/    [p setValue:@{<br \/>\n\/\/                  @&quot;&quot;name&quot;&quot; : @&quot;&quot;wangcai&quot;&quot;,<br \/>\n\/\/                  @&quot;&quot;money&quot;&quot;: @&quot;&quot;11.1&quot;&quot;,<br \/>\n\/\/<br \/>\n\/\/                  } forKey:@&quot;&quot;dog&quot;&quot;]; \/\/\u7ed9 dog\u8d4b\u503c\u4e00\u4e2a\u5b57\u5178\u80af\u5b9a\u662f\u4e0d\u5bf9\u7684<\/p>\n<p>(3)KVC\u89e3\u6790\u590d\u6742\u5b57\u5178\u7684\u6b63\u786e\u6b65\u9aa4<br \/>\n   NSDictionary *dict = @{<br \/>\n                       @&quot;&quot;name&quot;&quot; : @&quot;&quot;jack&quot;&quot;,<br \/>\n                       @&quot;&quot;money&quot;&quot;: @&quot;&quot;22.2&quot;&quot;,<br \/>\n                       @&quot;&quot;dog&quot;&quot; : @{<br \/>\n                               @&quot;&quot;name&quot;&quot; : @&quot;&quot;wangcai&quot;&quot;,<br \/>\n                               @&quot;&quot;price&quot;&quot;: @&quot;&quot;11.1&quot;&quot;,<br \/>\n                               },<br \/>\n                       \/\/\u4eba\u6709\u597d\u591a\u4e66<br \/>\n                       @&quot;&quot;books&quot;&quot; : @[<br \/>\n                               @{<br \/>\n                                   @&quot;&quot;name&quot;&quot; : @&quot;&quot;5\u5206\u949f\u7a81\u7834iOS\u5f00\u53d1&quot;&quot;,<br \/>\n                                   @&quot;&quot;price&quot;&quot; : @&quot;&quot;19.8&quot;&quot;<br \/>\n                                   },<br \/>\n                               @{<br \/>\n                                   @&quot;&quot;name&quot;&quot; : @&quot;&quot;3\u5206\u949f\u7a81\u7834iOS\u5f00\u53d1&quot;&quot;,<br \/>\n                                   @&quot;&quot;price&quot;&quot; : @&quot;&quot;24.8&quot;&quot;<br \/>\n                                   },<br \/>\n                               @{<br \/>\n                                   @&quot;&quot;name&quot;&quot; : @&quot;&quot;1\u5206\u949f\u7a81\u7834iOS\u5f00\u53d1&quot;&quot;,<br \/>\n                                   @&quot;&quot;price&quot;&quot; : @&quot;&quot;29.8&quot;&quot;<br \/>\n                                   }<br \/>\n                               ]<br \/>\n                       };<\/p>\n<p>    XMGPerson *p = [[XMGPerson alloc] init];<br \/>\n     p.dog = [[XMGDog alloc] init];<br \/>\n    [p.dog setValuesForKeysWithDictionary:dict[@&quot;&quot;dog&quot;&quot;]];<\/p>\n<p>    \/\/\u4fdd\u5b58\u6a21\u578b\u7684\u53ef\u53d8\u6570\u7ec4<br \/>\n    NSMutableArray *arrayM = [NSMutableArray array];<\/p>\n<p>    for (NSDictionary *dict in dict[@&quot;&quot;books&quot;&quot;]) {<br \/>\n        \/\/\u521b\u5efa\u6a21\u578b<br \/>\n        Book *book = [[Book alloc] init];<br \/>\n        \/\/KVC<br \/>\n        [book setValuesForKeysWithDictionary:dict];<br \/>\n        \/\/\u5c06\u6a21\u578b\u4fdd\u5b58<br \/>\n        [arrayM addObject:book];<br \/>\n    }<\/p>\n<p>         p.books = arrayM;<\/p>\n<p>\u5907\u6ce8:<br \/>\n    (1)\u5f53\u5b57\u5178\u4e2d\u7684\u952e\u503c\u5bf9\u5f88\u590d\u6742,\u4e0d\u9002\u5408\u7528KVC;<br \/>\n    (2)\u670d\u52a1\u5668\u8fd4\u8fd8\u7684\u6570\u636e,\u4f60\u53ef\u80fd\u4e0d\u4f1a\u5168\u7528\u4e0a,\u5982\u679c\u5728\u6a21\u578b\u4e00\u4e2a\u4e00\u4e2a\u5199\u5c5e\u6027\u975e\u5e38\u9ebb\u70e6,\u6240\u4ee5\u4e0d\u5efa\u8bae\u4f7f\u7528KVC\u5b57\u5178\u8f6c\u6a21\u578b<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;h4&gt;1.3.3 \u53d6\u503c&lt;\/h4&gt;<\/p>\n<p>(1) \u6a21\u578b\u8f6c\u5b57\u5178<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-objc&quot;&quot; lang=&quot;&quot;objc&quot;&quot;&gt; Person *p = [[Person alloc]init];<br \/>\n p.name = @&quot;&quot;jack&quot;&quot;;<br \/>\n p.money = 11.1;<br \/>\n \/\/KVC\u53d6\u503c<br \/>\n NSLog(@&quot;&quot;%@ %@&quot;&quot;, [p valueForKey:@&quot;&quot;name&quot;&quot;], [p valueForKey:@&quot;&quot;money&quot;&quot;]);<\/p>\n<p> \/\/\u6a21\u578b\u8f6c\u5b57\u5178, \u6839\u636e\u6570\u7ec4\u4e2d\u7684\u952e\u83b7\u53d6\u5230\u503c,\u7136\u540e\u653e\u5230\u5b57\u5178\u4e2d<br \/>\n NSDictionary *dict = [p dictionaryWithValuesForKeys:@[@&quot;&quot;name&quot;&quot;, @&quot;&quot;money&quot;&quot;]];<br \/>\n NSLog(@&quot;&quot;%@&quot;&quot;, dict);<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>(2) \u8bbf\u95ee\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u5c5e\u6027\u503c<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-objc&quot;&quot; lang=&quot;&quot;objc&quot;&quot;&gt;Book *book1 = [[Book alloc] init];<br \/>\nbook1.name = @&quot;&quot;5\u5206\u949f\u7a81\u7834iOS\u5f00\u53d1&quot;&quot;;<br \/>\nbook1.price = 10.7;<\/p>\n<p>Book *book2 = [[Book alloc] init];<br \/>\nbook2.name = @&quot;&quot;4\u5206\u949f\u7a81\u7834iOS\u5f00\u53d1&quot;&quot;;<br \/>\nbook2.price = 109.7;<\/p>\n<p>Book *book3 = [[Book alloc] init];<br \/>\nbook3.name = @&quot;&quot;1\u5206\u949f\u7a81\u7834iOS\u5f00\u53d1&quot;&quot;;<br \/>\nbook3.price = 1580.7;<\/p>\n<p>\/\/ \u5982\u679cvalueForKeyPath:\u65b9\u6cd5\u7684\u8c03\u7528\u8005\u662f\u6570\u7ec4\uff0c\u90a3\u4e48\u5c31\u662f\u53bb\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u7684\u5c5e\u6027\u503c<br \/>\n\/\/ \u53d6\u5f97books\u6570\u7ec4\u4e2d\u6240\u6709Book\u5bf9\u8c61\u7684name\u5c5e\u6027\u503c\uff0c\u653e\u5728\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u4e2d\u8fd4\u56de<br \/>\n    NSArray *books = @[book1, book2, book3];<br \/>\n    NSArray *names = [books valueForKeyPath:@&quot;&quot;name&quot;&quot;];<br \/>\n    NSLog(@&quot;&quot;%@&quot;&quot;, names);<\/p>\n<p>\/\/\u8bbf\u95ee\u5c5e\u6027\u6570\u7ec4\u4e2d\u5143\u7d20\u7684\u5c5e\u6027\u503c<br \/>\nPerson *p = [[Person alloc]init];<br \/>\np.books = @[book1, book2, book3];<br \/>\nNSArray *names = [p valueForKeyPath:@&quot;&quot;books.name&quot;&quot;];<br \/>\nNSLog(@&quot;&quot;%@&quot;&quot;, names);<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;h2&gt;2 KVO (Key Value Observing)&lt;\/h2&gt;<\/p>\n<p>&lt;h3&gt;2.1 KVO \u7684\u5e95\u5c42\u5b9e\u73b0\u539f\u7406&lt;\/h3&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-objc&quot;&quot; lang=&quot;&quot;objc&quot;&quot;&gt;(1)KVO \u662f\u57fa\u4e8e runtime \u673a\u5236\u5b9e\u73b0\u7684<br \/>\n(2)\u5f53\u4e00\u4e2a\u5bf9\u8c61(\u5047\u8bbe\u662fperson\u5bf9\u8c61,\u5bf9\u5e94\u7684\u7c7b\u4e3a JLperson)\u7684\u5c5e\u6027\u503cage\u53d1\u751f\u6539\u53d8\u65f6,\u7cfb\u7edf\u4f1a\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u7ee7\u627f\u81eaJLperson\u7684\u7c7bNSKVONotifying_JLPerson,\u5728\u8fd9\u4e2a\u7c7b\u7684 setAge \u65b9\u6cd5\u91cc\u9762\u8c03\u7528<br \/>\n    [super setAge:age];<br \/>\n    [self willChangeValueForKey:@&quot;&quot;age&quot;&quot;];<br \/>\n    [self didChangeValueForKey:@&quot;&quot;age&quot;&quot;];<br \/>\n \u4e09\u4e2a\u65b9\u6cd5,\u800c\u540e\u9762\u4e24\u4e2a\u65b9\u6cd5\u5185\u90e8\u4f1a\u4e3b\u52a8\u8c03\u7528<br \/>\n -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary&lt;NSString *,id&gt; *)change context:(void *)context\u65b9\u6cd5,\u5728\u8be5\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u62ff\u5230\u5c5e\u6027\u6539\u53d8\u524d\u540e\u7684\u503c.<br \/>\n&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;h3&gt;2.2 KVO\u7684\u4f5c\u7528&lt;\/h3&gt;<\/p>\n<p>&lt;ul&gt;<br \/>\n&lt;li&gt;\u4f5c\u7528:\u80fd\u591f\u76d1\u542c\u67d0\u4e2a\u5bf9\u8c61\u5c5e\u6027\u503c\u7684\u6539\u53d8&lt;\/li&gt;<br \/>\n&lt;\/ul&gt;<\/p>\n<p>\u00a0<\/p>\n<p>&lt;pre&gt;&lt;code class=&quot;&quot;language-objc&quot;&quot; lang=&quot;&quot;objc&quot;&quot;&gt;\/\/ \u5229\u7528KVO\u76d1\u542cp\u5bf9\u8c61name \u5c5e\u6027\u503c\u7684\u6539\u53d8<br \/>\n    Person *p = [[XMGPerson alloc] init];<br \/>\n    p.name = @&quot;&quot;jack&quot;&quot;;<\/p>\n<p>   \/* \u5bf9\u8c61p\u6dfb\u52a0\u4e00\u4e2a\u89c2\u5bdf\u8005\uff08\u76d1\u542c\u5668\uff09<br \/>\n     Observer\uff1a\u89c2\u5bdf\u8005\uff08\u76d1\u542c\u5668\uff09<br \/>\n     KeyPath\uff1a\u5c5e\u6027\u540d\uff08\u9700\u8981\u76d1\u542c\u54ea\u4e2a\u5c5e\u6027\uff09<br \/>\n     *\/<br \/>\n    [p addObserver:self forKeyPath:@&quot;&quot;name&quot;&quot; options:NSKeyValueObservingOptionNew  | NSKeyValueObservingOptionOld context:@&quot;&quot;123&quot;&quot;];<\/p>\n<p> \/**<br \/>\n *  \u5229\u7528KVO \u76d1\u542c\u5230\u5bf9\u8c61\u5c5e\u6027\u503c\u6539\u53d8\u540e,\u5c31\u4f1a\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5<br \/>\n *<br \/>\n *  @param keyPath \u54ea\u4e00\u4e2a\u5c5e\u6027\u88ab\u6539\u4e86<br \/>\n *  @param object  \u54ea\u4e00\u4e2a\u5bf9\u8c61\u7684\u5c5e\u6027\u88ab\u6539\u4e86<br \/>\n *  @param change  \u6539\u6210\u4ec0\u4e48\u6837\u4e86<br \/>\n *\/<br \/>\n&#8211; (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary&lt;NSString *,id&gt; *)change context:(void *)context<br \/>\n{<br \/>\n    \/\/ NSKeyValueChangeNewKey == @&quot;&quot;new&quot;&quot;<br \/>\n    NSString *new = change[NSKeyValueChangeNewKey];<br \/>\n    \/\/ NSKeyValueChangeOldKey == @&quot;&quot;old&quot;&quot;<br \/>\n    NSString *old = change[NSKeyValueChangeOldKey];<\/p>\n<p>    NSLog(@&quot;&quot;%@-%@&quot;&quot;,new,old);<br \/>\n}<\/p>\n<p>&lt;\/code&gt;&lt;\/pre&gt;<\/p>\n<p>&lt;pre&gt;&lt;code&gt;            &quot;&#8220;`<br \/>\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`&#8221; &lt;h2&gt;1 KVC(KeyValueCoding)&lt;\/h2&#038; [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-42411","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/42411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=42411"}],"version-history":[{"count":1,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/42411\/revisions"}],"predecessor-version":[{"id":42412,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/42411\/revisions\/42412"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=42411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=42411"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=42411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}