参数from observeValueForKeyPath:ofObject:change:context: [英] Parameters from observeValueForKeyPath:ofObject:change:context:

查看:152
本文介绍了参数from observeValueForKeyPath:ofObject:change:context:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这个方法的参数将返回。

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

文档



对象键路径keyPath的源对象。



更改一个字典,用于描述对关键路径中的属性的
值所做的更改
相对于对象的keyPath。



上下文当接收者注册到
时提供的值接收键值观察
通知。 / p>

您可以为我解释这些参数,因为我发现文档有点难以理解。

解决方案

注册KVO通知时,您指定了 addObserver:的键入。 keypath参数只是返回给你的这个值。对象参数是您发送 addObserver:消息的对象。这些可以用于区分不同关键路径/对象的KVO通知(例如,如果您观察多个值)。



change 是包含有关值更改的性质的信息的字典。它可能包含新值或旧值,或者对于对多关系,它可能包含已更改的索引。其内容在 $



当您注册时,您可以在更改接收更改通知中查看rel =nofollow noreferrer> KVO Programming Guide 。通知您还可以指定上下文值。最后一个值就是这个值返回给你。如果您没有任何特定于上下文的信息,则将nil传递给 addObserver:是适当的。



关于KVO系统的一些缺点(以及一些帮助类来解决它们)的好讨论,见 Mike Ash的精彩博文


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.

解决方案

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

这篇关于参数from observeValueForKeyPath:ofObject:change:context:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆