2009-07-08 113 views
2

我试图用KVC更新一些属性。这些属性已经被合成。可可KVC:“类不是关键值编码兼容”

这条线的工作原理:

myObject.value = intValue; 

这不起作用:

[self setValue:[NSNumber numberWithInt:intValue] forKey:@"myObject.value"]; 

而且随着炸毁:终止应用程序由于未捕获的异常 'NSUnknownKeyException',原因:“[< MyViewController 0xd1cec0> setValue:forUndefinedKey:]:该类不是关键字值myObject.value。

更进一步的方法(awakeFromNib)同一类的其他实例对setValue:forKey:调用作出了很好的响应。唯一的区别是这个特定的实例是在IB中创建和连接的。

+0

[NSString stringWithFormat:@“myObject.value”]是多余的,它使用的格式没有任何参数。你应该使用@“myObject.value”。 –

回答

7

它告诉你,该对象不是一个有效的键,事实上它不是:“myObject.value”是一个键路径,而不是一个单一的键。

+0

doh。我知道......谢谢! – Meltemi

1

我同意查克。

我认为你需要做的,而不是:

[self setValue:[NSNumber numberWithInt:intValue] forKeyPath:@"myObject.value"]; 

或通过像关键路径的各个部分:

id myObject = [self objectForKey:@"myObject"]; 
[myObject setValue:[NSNumber numberWithInt:intValue] forKey:@"value"]; 
9

你不可错过的关键路径作为第二个参数-[NSObject setValue:forKey:]。你想用setValue:forKeyPath:代替:

[self setValue:[NSNumber numberWithInt:intValue] forKeyPath:@"myObject.value"]; 

我的理解是,setValue:forKey:作为一个性能优化提供。由于它不能采取关键路径,因此不必解析密钥字符串。

相关问题