KVO不适用于com.alpha之类的键路径. [英] KVO doesn't work with keypath like com.alpha.
问题描述
我的NSMutableDictionary包含简单键(@一个",@两个",@三个")和复杂键(@"com.alpha",@"com.beta").可以为复杂的密钥使用观察者吗?
My NSMutableDictionary contains simple keys (@"one", @"two", @"three") and complex keys (@"com.alpha", @"com.beta"). Is it possible to use observers for a complex key?
观察者使用简单的键可以很好地工作,但是不能使用复杂的键.什么是解决方案?
Observers work well with simple keys, but didn't worked with complex keys. What is a solution?
[self.dict addObserver:self forKeyPath:@"com.alpha" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
-(IBAction) onChange:(id)sender
{
[self.dict setObject:@"newValue" forKey:@"com.alpha"];
}
-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"____ value had changed");
}
推荐答案
不能将包含点.
的键用于键值编码或键值观察.该点用于构建关键路径,该路径用于指定要遍历的对象属性序列. (请参见键和键路径.)
You cannot use keys containing a dot .
with key-value-coding or key-value-observing. The dot is used to build a key path that is used to specify a sequence of object properties to traverse. (See Keys and Key Paths in the "Key-Value Coding Programming Guide".)
例如,
id x = [object valueForKeyPath:@"com.alpha"];
与
id x = [[object valueForKey:@"com"] valueForKey:@"alpha"];
对于单个键"com.alpha",您必须将其重命名为例如"com_alpha".
For a single key "com.alpha", you will have to rename it to e.g. "com_alpha".
这篇关于KVO不适用于com.alpha之类的键路径.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!