views:

469

answers:

1

I was wondering what the parameters from this method would return.

- (void) observeValueForKeyPath:(NSString *)keyPath
                       ofObject:(id)object
                         change:(NSDictionary *)change
                        context:(void *)context;

In the documentation it says

keyPath The key path, relative to object, to the value that has changed.

object The source object of the key path keyPath.

change A dictionary that describes the changes that have been made to the value of the property at the key path keyPath relative to object.

context The value that was provided when the receiver was registered to receive key-value observation notifications.

Could you possibly explain these parameters for me, as i find the documentation a bit hard to understand.

+5  A: 

When you registered for KVO notifications you specified a keypath to addObserver:. The keypath parameter is simply this value being returned to you. The object parameter is the object to which you sent the addObserver: message. These can be used to differentiate between KVO notifications of different keypaths/objects (for instance if you're observing multiple values).

change is a dictionary that contains information about the nature of the value change. It might contain the new value or the old value or, for to-many relationships, it might contain the indices that changed. Its contents are better described in the KVO Programming Guide in the Receiving Notification of a Change section.

When you register for the notification you can also specify a context value. The last value is simply this value returned to you. If you don't have any context-specific information, passing nil to addObserver: is appropriate.

For a good discussion on some shortcomings of the KVO system (and some helper classes to address them), see Mike Ash's great blog post

nall