Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Observing Keys

From: kvo.m

 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.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint