NSCountedSet 上的 KVO?
我想监视 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
直接与设置的对象对话不会发出 KVO 更改通知。您需要以符合 KVC 的方式更改属性的设置值。有两种方法:
mutableSetValueForKey:
消息。这将为您提供一个假的 set 对象,该对象包装属性并针对您对其所做的每个更改发布 KVO 通知。[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:
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.[myCountedSet addObject:foo]
(except inaddCountedSetObject:
); 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).
无序对多关系肯定有 KVO 手动更改方法。
您不想设置 选项 为非零?例如,
NSKeyValueObservingOptionNew
还有 Mike Ash 的 KVO Helper 非常出色。
来自 addObserver 上的 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:
需要检查的一些事情:
myController
是否非nil
?如果为nil
,则addObserver::::
消息会静静地落在地板上。Some things to check:
myController
non-nil
? If it'snil
, theaddObserver::::
message simply falls on the floor silently.