KVO+Blocks
by Hans Pinckaers
Just found out this little NSObject category. It’s awesome. A must #include in your project.
“To get observation notices, you have to override a lengthy selector (observeValueForKeyPath:ofObject:change:context:), provide a static context pointer, and essentially implement a big switch statement on the key path.
That’s unwieldy, but I think it also makes for unmaintainable code: the callbacks end up thrown in the same method, and they’re separated from the observer registration.”
~ KVO+Blocks: Block Callbacks for Cocoa Observers
Since iOS 4 support blocks, it’s possible to use this for iPhone projects too.
There’s a comprehensive discussion of KVO’s limitations, and some code that works around them, in Michael Ash’s blog at http://www.mikeash.com/pyblog/key-value-observing-done-right.html . I posted in the comments of that entry with a version of his code that adds blocks support.
I’m acquainted with Andy Matuschak – he works on UIKit at Apple. You would be remiss if you did not also include his follow-up link:
http://blog.andymatuschak.org/private/1070660478/tumblr_l8acac1Dlg1qzk3gw