Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
|
Code View:
Scroll
/
Show All 5 + (void)observeValueForKeyPath: 6 (NSString*)keyPath 7 ofObject: (id)object 8 change: (NSDictionary*)change 9 context: (void*)context 10 { 11 NSLog(@"%@.%@ is now %@", object, keyPath, 12 [change objectForKey: NSKeyValueChangeNewKey ]); 13 } 14 + (void)watchChange 15 { 16 NSMutableDictionary *dict = 17 [NSMutableDictionary new]; 18 [dict addObserver: self 19 forKeyPath: @"aKey" 20 options: 21 NSKeyValueObservingOptionNew 22 context: NULL]; 23 24 [dict setObject: @"set as object" 25 forKey: @"aKey"]; 26 27 [dict removeObserver: self 28 forKeyPath: @"aKey"]; |
Having a uniform mechanism for setting and getting keys is useful, but it’s not particularly special. The real power of KVC comes from KVO, which lets you monitor keys for changes.