NSMutableDictionary 键值对每个键都兼容吗?
我需要在很多不同的键上设置一些观察者,并且我不想声明大约五十个不同的@properties(正如我已经开始做的那样),所以我想知道 NSDictionary 是否可以为我完成这一切?
更新
猜测我应该更具体,我希望能够在 NSMutableDictionary 中为任意键(NSStrings)设置对象,然后也为同一字典中的任意键注册观察者,无论该键之前是否已在该字典中设置过字典。 NSMutableDictionary 是否以“开箱即用”的方式工作?或者我是否需要创建一个覆盖 setValue:forKey: 的容器类?
更新 2
我将这个快速测试放在一起,看起来很有希望。这里使用SenTestingKit进行测试。
//My dictionary
NSMutableDictionary * myDict = [[NSMutableDictionary alloc] init];
//An object that does not respond to notifications, and will therefore throw exceptions
NSNumber * notKVOcompliant = [NSNumber numberWithInt:1];
//Adding that object as an observer...
[myDict addObserver:notKVOcompliant forKeyPath:@"five" options:NSKeyValueObservingOptionNew context:nil];
//causing a notification to fire, which causes an exception to throw.
STAssertThrows([myDict setObject:@"something" forKey:@"five"],@"Notification was not sent.");
//A control group, to make sure the exception is because of the specific observed key
STAssertNoThrow([myDict setObject:@"something" forKey:@"six"], @"Control group");
//and lastly a manufactured failure to make sure this test has run
STFail(@"Ensure this test is running");
所以我运行了它,看起来它确实可以开箱即用。 NSNumber 作为观察者必须获取通知,这会导致它抛出无法识别的选择器异常,从而导致 STAssertThrows 通过。
另一个键没有观察者,因此没有效果。
STFail 失败,这意味着测试肯定会运行。
I need to set some observers on a lot of different keys and I'd rather not declare about fifty different @properties, (as I had started to do) so I'm wondering if NSDictionary can do it all for me?
Update
Guess I should be more specific, I would like to be able to set objects for arbitrary keys (NSStrings) in an NSMutableDictionary, then also registerObserver for arbitrary keys in the same dictionary, regardless of whether the key has ever been set before in that dict. Does an NSMutableDictionary work this way "out of the box"? or do I need to make a container class with overridden setValue:forKey: ?
Update 2
I threw together this quick test and it looks promising. Using SenTestingKit here to test.
//My dictionary
NSMutableDictionary * myDict = [[NSMutableDictionary alloc] init];
//An object that does not respond to notifications, and will therefore throw exceptions
NSNumber * notKVOcompliant = [NSNumber numberWithInt:1];
//Adding that object as an observer...
[myDict addObserver:notKVOcompliant forKeyPath:@"five" options:NSKeyValueObservingOptionNew context:nil];
//causing a notification to fire, which causes an exception to throw.
STAssertThrows([myDict setObject:@"something" forKey:@"five"],@"Notification was not sent.");
//A control group, to make sure the exception is because of the specific observed key
STAssertNoThrow([myDict setObject:@"something" forKey:@"six"], @"Control group");
//and lastly a manufactured failure to make sure this test has run
STFail(@"Ensure this test is running");
So I ran that, and it seems like it actually DOES work out of the box. The NSNumber as an observer must be getting the notification, which causes it to throw an unrecognized selector exception, which causes STAssertThrows to pass.
The other key has no observers, and therefore no effect.
The STFail is failing, which means the test is for sure running.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。当使用
setValue:forKey:
更改键时,普通的非子类 NSMutableDictionary 会为订阅的任何 NSString 键传递 KVO 通知Yes, it is. A normal un-subclassed NSMutableDictionary delivers KVO notifications for any NSString key that is subscribed to, when the key is changed with
setValue:forKey: