Cocoa 中的关键值观察,内省更改属性

发布于 2024-07-19 19:34:08 字数 371 浏览 6 评论 0原文

我正在使用 NSObject 方法对布尔属性进行键值观察:

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

此键路径的值中最有趣的部分是一个 BOOL,它不断在 YES/NO 之间翻转。 我从更改字典中得到的最多的是 kind = 1。有没有办法不探测我正在观察的对象来查看实际的更改值是什么?

谢谢。

I'm using key value observing on a boolean property an NSObject method:

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

The most interesting part of the value for this key path is a BOOL which is constantly flipping between YES/NO. The most I get out of the change dictionary is kind = 1. Is there anyway without probing the object I'm observing to see what the actual change value is?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天荒地未老 2024-07-26 19:34:08

首先,您指定 NSKeyValueObservingOptionNew:

[theObject addObserver: self
            forKeyPath: @"theKey"
               options: NSKeyValueObservingOptionNew
               context: NULL];

…然后,在您的观察者方法中:

-(void) observeValueForKeyPath: (NSString *)keyPath ofObject: (id) object
                        change: (NSDictionary *) change context: (void *) context
{
    BOOL newValue = [[change objectForKey: NSKeyValueChangeNewKey] boolValue];
}

理想情况下,您会在调用 - 之前检查 value 是否为 nil (好吧,它可能会发生) boolValue,但为了清楚起见,此处省略了该值。

Firstly, you specify NSKeyValueObservingOptionNew:

[theObject addObserver: self
            forKeyPath: @"theKey"
               options: NSKeyValueObservingOptionNew
               context: NULL];

…then, in your observer method:

-(void) observeValueForKeyPath: (NSString *)keyPath ofObject: (id) object
                        change: (NSDictionary *) change context: (void *) context
{
    BOOL newValue = [[change objectForKey: NSKeyValueChangeNewKey] boolValue];
}

Ideally you'd check whether value was nil (well, it might happen) before calling -boolValue, but that was omitted for clarity here.

梦过后 2024-07-26 19:34:08

正如 Jim Dovey 所说,除了更改字典不会带来 nil,而是 null 值,因此这

NSLog(@"%@", [change description]); 

将导致类似的结果:

{
    kind = 1;
    new = <null>;
    old = <null>;
}

如前所述,对 null 值调用 boolValue 将导致错误

[NSNull boolValue]:无法识别的选择器发送到实例 0xa0147020

为了避免这种情况,必须检查 [NSNull null] 而不是 nil,如下所示

if([change objectForKey:NSKeyValueChangeNewKey] != [NSNull null]) 
  BOOL newValue = [[change objectForKey: NSKeyValueChangeNewKey] boolValue];

id newValue;
if((newValue[change valueForKey: @"new"]) != [NSNull null]){
     BOOL newBOOL = [newValue boolValue];
}

As Jim Dovey says, except that the change dictionary does not bring nil, but null values, so that

NSLog(@"%@", [change description]); 

will result in something like:

{
    kind = 1;
    new = <null>;
    old = <null>;
}

As mentioned, calling boolValue on a null value will result in an error

[NSNull boolValue]: unrecognized selector sent to instance 0xa0147020

To avoid this, one has to check not for nil but for [NSNull null], like so:

if([change objectForKey:NSKeyValueChangeNewKey] != [NSNull null]) 
  BOOL newValue = [[change objectForKey: NSKeyValueChangeNewKey] boolValue];

or

id newValue;
if((newValue[change valueForKey: @"new"]) != [NSNull null]){
     BOOL newBOOL = [newValue boolValue];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文