在 KVO 中使用嵌套键路径

Using nested key paths in KVO

假设我想在自己的名为 'controller' 的 属性 上观察名为“isEnabled”的 属性。据我所知,我有两种安装此类观察的选择:

1. [self.controller addObserver:self forKeyPath:@"isEnabled" options:0 context:nil];
2. [self addObserver:self forKeyPath:@"controller.isEnabled" options:0 context:nil];

我注意到这两种方法之间的实际区别——在第二种方法中,如果 self 上的 'controller' 对象被替换,我会收到通知,而在第一种方法中,我只会在 'isEnabled' 属性 在我安装观察的同一个实例上发生了变化。

我的问题是,如果有的话,这到底是在哪里记录的?我知道它有效,但我应该使用它吗?
我在 Apple 文档中找不到任何提及此类行为的信息,尽管其他一些人在论坛中提到了它。任何参考都将很乐意接受。

谢谢。

不仅仅是 controller 属性 发生变化时您会收到更改通知,KVO 将切换到跟踪 isEnabled 属性新控制器并停止跟踪旧控制器的 isEnabled 属性。

这隐含在关键路径的概念中。键值观察建立在键值编码之上。 Key-Value Coding Programming Guide 对 Key-Value Coding Fundamentals:

中的键路径说了这个

A key path is a string of dot separated keys that is used to specify a sequence of object properties to traverse. The property of the first key in the sequence is relative to the receiver, and each subsequent key is evaluated relative to the value of the previous property.

For example, the key path address.street would get the value of the address property from the receiving object, and then determine the street property relative to the address object.

-addObserver:forKeyPath:options:context: 的意思不是“跟随关键路径到最后一个元素并在倒数第二个对象上观察 属性”。它是“观察接收者对象的这个关键路径”。关键路径始终被认为是从接收者开始的。

换句话说,对于你的第二个代码示例,它不是“观察 isEnabled 属性 of the controller of self”(这就是你的意思第一个例子)。意思是“观察 selfcontroller.isEnabled。任何时候表达式 [self valueForKeyPath:@"controller.isEnabled"] 的计算结果已经或可能已经改变,通知我。”

I know it works but should I use it?

是的,你应该使用它。这是 API 的预期含义。这就是为什么该方法将其参数描述为键 path 而不仅仅是键。

on the second approach i will get a notification if the 'controller' object on self was replaced

我会换句话说,在第二种方法中,如果控制器对象被替换 或者它的 isEnabled 属性 发生变化 ,您将收到通知.换句话说,当 controller.isEnabled 发生变化时(正如 Ken 的回答所解释的那样)。

检查这个例子:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.controller = [[ViewController2 alloc] init];

    [self.controller addObserver:self forKeyPath:@"isEnabled" options:NSKeyValueObservingOptionNew context:nil];
    [self addObserver:self forKeyPath:@"controller.isEnabled" options:NSKeyValueObservingOptionNew context:nil];

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.controller.isEnabled = !self.controller.isEnabled;

        // replace controller
        [self.controller removeObserver:self forKeyPath:@"isEnabled"];
        self.controller = [[ViewController2 alloc] init];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.controller.isEnabled = !self.controller.isEnabled;
        });
    });
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSLog(@"KVO %d %@ %p", self.controller.isEnabled, keyPath, self.controller);
}

我们将收到 4 个 KVO 通知:

KVO 1 controller.isEnabled 0x7fbbc2e4b4e0 <-- These 2 fire together when we toggle isEnbled
KVO 1 isEnabled 0x7fbbc2e4b4e0            <-- So basically 1. and 2. behave the same
KVO 0 controller.isEnabled 0x7fbbc2e58d30 <---- controller was replaced
KVO 1 controller.isEnabled 0x7fbbc2e58d30 <---- Toggle on the new instance