这个问题已经在这里有了答案:
7年前关闭。
我正在使用字典,并且想向自定义类中添加键/值,但是我总是会收到错误消息,该类不符合KVC,但是Apple文档指出应该如此。
我的代码:
ContactObject.h:
@interface ContactObject : NSObject
+ (ContactObject *)testAdding;
@end
ContactObject.m:
@implementation ContactObject
- (id)init {
self = [super init];
if (self) {
// customize
}
return self;
}
+ (ContactObject *)testAdding
{
// create object
ContactObject *theReturnObject = [[ContactObject alloc] init];
[theReturnObject setValue:@"Berlin" forKey:@"city"];
[theReturnObject setValue:@"Germany" forKey:@"state"];
return theReturnObject;
}
@end
我想我错过了一些非常愚蠢的东西:)
请任何帮助表示赞赏...
问候,
马蒂亚斯
最佳答案
实际上符合KVC:
key
。 setKey:
。 (如果属性是Boolean
属性,则getter访问器方法的形式为isKey
。) key
或_key
。 我看不到这三个实现中的任何一个。您至少需要具有要通过KVC设置的属性,默认的NSObject实现可以通过
setValue:forKey:
设置属性,但是必须声明它们。关于objective-c - 自定义类NSObject不符合键值编码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14176353/