Key-Value Observing Done Right
It’s too bad that such a powerful tool is so broken. Especially since Apple is starting a trend of omitting traditional NSNotification and delegate callbacks in new APIs, instead simply supporting KVO. A perfect example of this is NSOperation: the only way to get notified when an NSOperation finishes is by using KVO to watch its
isFinished
property.
I hope those are mistakes and not part of a trend. In any case, Ash has done what I was planning to do the first time I couldn’t avoid using KVO: write an adapter that hides the madness behind a notification center interface. It’s simple and clean:
It takes advantage of one pointer that can be guaranteed to be unique: the ‘self’ pointer. Instead of registering the target object for a notification, it creates a unique helper object for each notification and registers that. The helper then receives the notification and bounces it back to the original observer.