Parameters from observeValueForKeyPath:ofObject:change:context:

前端 未结 1 1974
失恋的感觉
失恋的感觉 2020-12-31 06:41

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

- (void) observeValueForKeyPath:(NSString *)keyPath
                       ofObject:(id)obj         


        
1条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 07:16

    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

    0 讨论(0)
提交回复
热议问题