自定义类NSObject不符合键值编码 [英] Custom class NSObject not key value coding compliant
问题描述
可能重复:
为什么我的对象不符合键值编码?
Possible Duplicate:
Why is my object not key value coding-compliant?
我正在使用字典,我想将键/值添加到自定义类中,但是我总是会收到错误消息,该类不符合KVC,但是Apple文档指出应该如此.
I'm having a dictionary and I want to add keys/values to a custom class, but i always get the error, that the class is not KVC compliant, but the Apple documents state that it should be.
我的代码:
ContactObject.h:
ContactObject.h:
@interface ContactObject : NSObject
+ (ContactObject *)testAdding;
@end
ContactObject.m:
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
我认为我缺少一些非常愚蠢的东西:)
I think I'm missing something very stupid :)
请,任何帮助表示赞赏...
Please, any help appreciated ...
问候, 马蒂亚斯
推荐答案
实际上符合KVC:
如何使属性符合KVC取决于该属性是属性,一对一关系还是一对多关系.对于属性和一对一关系,类必须按照给定的优先顺序(键表示属性键)实现以下至少一项:
How you make a property KVC compliant depends on whether that property is an attribute, a to-one relationship, or a to-many relationship. For attributes and to-one relationships, a class must implement at least one of the following in the given order of preference (key refers to the property key):
- 该类具有名称为
key
的声明属性. - 它实现名为key的访问器方法,如果属性是可变的,则实现
setKey:
. (如果属性是Boolean
属性,则getter访问器方法的形式为isKey
.) - 它以
key
或_key
的形式声明一个实例变量.
- The class has a declared property with the name
key
. - It implements accessor methods named key and, if the property is mutable,
setKey:
. (If the property is aBoolean
attribute, the getter accessor method has the formisKey
.) - It declares an instance variable of the form
key
or_key
.
我看不到这三个实现中的任何一个.您至少需要具有要通过KVC设置的属性,默认的NSObject实现可以通过setValue:forKey:
设置属性,但是必须声明它们.
I don't see any of these three implemented. You need to have at least properties that you are trying to set through KVC, the default NSObject implementation is able to set properties through setValue:forKey:
but you must declare them.
这篇关于自定义类NSObject不符合键值编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!