为什么我的对象不符合键值编码?

时间:2012-07-15 12:37:05

标签: objective-c cocoa key-value-coding

尝试使用键值编码在我的对象上设置值:

[engine setValue:[NSNumber numberWithInt:horsePower]
          forKey:@"horsePower"];

导致错误:

[<Slant6 0x7fbc61c15c40> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key horsePower.

我需要做些什么才能使engine符合键值编码?

2 个答案:

答案 0 :(得分:4)

确保engine所属的类实现horsePower属性,和/或具有horsePower实例变量,和/或手动实现setHorsePower:和{{1方法。

您收到错误是因为您尝试设置horsePower密钥的值,但horsePower的类未正确实现设置engine密钥的方法。

答案 1 :(得分:3)

有很多bunch of approaches利用KVC - 也许最简单的方法是在你的班级中使用你想要使用的密钥的名称来设置一个实例变量,i。 E:

@interface Engine: NSObject {
    NSNumber *horsePower;
    // etc.
}