NSCountedSet 上的 KVO?

发布于 2024-08-08 20:29:15 字数 766 浏览 8 评论 0原文

我想监视 NSCountedSet 以查看其内容是否发生变化。设置 KVO 似乎可以编译,但没有被触发。第一个问题:你能观察一个集合吗?如果是这样,那么这条消息有什么问题吗?

    [subViewA addObserver:subViewB forKeyPath:@"countedSet" options:0 context:NULL];

我真的只是想监视集合中(对象数量)的计数(如果有帮助的话)。

编辑 - 这是观察者 (subViewB):

- (void)observeValueForKeyPath:(NSString *)keyPath 
                      ofObject:(id)object 
                        change:(NSDictionary *)change 
                       context:(void *)context {
    if ([keyPath isEqual:@"countedSet"]) {
        NSLog(@"Set has changed");
    }
}

Edit2 - 将 addObserver 消息从 subView 移动到 viewController。所以我试图让一个子视图观察另一个 viewController 的子视图中的 NSCountedSet 。关键路径是“相对于接收器”——我假设它是 subViewA。

I'd like to monitor an NSCountedSet to see if its contents changes. Setting up KVO seems to compile but it's not being triggered. First question: can you observe a set? If so then is there something wrong with this message?

    [subViewA addObserver:subViewB forKeyPath:@"countedSet" options:0 context:NULL];

I'm really just trying to monitor the count of (number of objects in) the set if that helps.

Edit - here's the observer (subViewB):

- (void)observeValueForKeyPath:(NSString *)keyPath 
                      ofObject:(id)object 
                        change:(NSDictionary *)change 
                       context:(void *)context {
    if ([keyPath isEqual:@"countedSet"]) {
        NSLog(@"Set has changed");
    }
}

Edit2 - moved the addObserver message from the subView to the viewController. So I'm trying to get one subView to observe a NSCountedSet in another of the viewController's subViews. key path is "relative to the receiver" - which I assume to be subViewA.

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

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

发布评论

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

评论(3

肥爪爪 2024-08-15 20:29:15

直接与设置的对象对话不会发出 KVO 更改通知。您需要以符合 KVC 的方式更改属性的设置值。有两种方法:

  1. 向属性所有者发送 mutableSetValueForKey: 消息。这将为您提供一个假的 set 对象,该对象包装属性并针对您对其所做的每个更改发布 KVO 通知。
  2. 实现属性的 set 访问器方法,并在任何地方使用它们。每个方法的实现都直接与底层集合对象对话;不在这些方法之一中的所有代码都应该通过它们。因此,例如,要添加对象,不应使用 [myCountedSet addObject:foo]addCountedSetObject: 中除外);您应该使用 [self addCountedSetObject:foo] 代替。

我推荐#2。这听起来可能需要更多工作,但其实并不多,而且它可以编写出非常好的代码。

更多详细信息模型对象实现指南核心数据编程指南(尽管这不是特定于 Core Data 的)。

Talking directly to the set object does not issue KVO change notifications. You need to make changes to the property's set value in a KVC-compliant way. There are two ways:

  1. Send the property owner a mutableSetValueForKey: message. This will give you a fake set object that wraps the property and posts KVO notifications around each change you make to it.
  2. Implement the set accessor methods for the property, and use them everywhere. The implementation of each method talks directly to the underlying set object; all code that isn't in one of these methods should go through them. So, for example, to add an object, you should not use [myCountedSet addObject:foo] (except in addCountedSetObject:); you should use [self addCountedSetObject:foo] instead.

I recommend #2. It may sound like more work, but it's not much, and it makes for really good code.

More details in the Model Object Implementation Guide and in the Core Data Programming Guide (even though this is not specific to Core Data).

有深☉意 2024-08-15 20:29:15

无序对多关系肯定有 KVO 手动更改方法。

您不想设置 选项 为非零?例如,NSKeyValueObservingOptionNew

还有 Mike Ash 的 KVO Helper 非常出色。

来自 addObserver 上的 NSSet 文档:

NSSet 对象是不可观察的,所以
当以下情况时此方法会引发异常
在 NSSet 对象上调用。而不是
观察集合,观察无序
集合的对多关系
是相关对象的集合。

There are definitely KVO manual change methods for unordered to-many relationships.

Don't you want to be setting your options to non-zero? For instance, NSKeyValueObservingOptionNew

Also Mike Ash's KVO Helper is pretty excellent.

From the NSSet docs on addObserver:

NSSet objects are not observable, so
this method raises an exception when
invoked on an NSSet object. Instead of
observing a set, observe the unordered
to-many relationship for which the set
is the collection of related objects.

绝對不後悔。 2024-08-15 20:29:15

需要检查的一些事情:

  1. myController 是否非nil?如果为 nil,则 addObserver:::: 消息会静静地落在地板上。
  2. 你的方法被调用了吗?也许它正在被调用,但不是使用您期望的关键路径。 (我也没想到会这样,但值得检查。)

Some things to check:

  1. Is myController non-nil? If it's nil, the addObserver:::: message simply falls on the floor silently.
  2. Is your method getting called at all? Perhaps it's being called, but not with the key path you expect. (I wouldn't expect this, either, but it's worth checking.)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文