自定义类NSObject不符合键值编码 [英] Custom class NSObject not key value coding compliant

查看:104
本文介绍了自定义类NSObject不符合键值编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
为什么我的对象不符合键值编码?

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):

  1. 该类具有名称为key的声明属性.
  2. 它实现名为key的访问器方法,如果属性是可变的,则实现setKey:. (如果属性是Boolean属性,则getter访问器方法的形式为isKey.)
  3. 它以key_key的形式声明一个实例变量.
  1. The class has a declared property with the name key.
  2. It implements accessor methods named key and, if the property is mutable, setKey:. (If the property is a Boolean attribute, the getter accessor method has the form isKey.)
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆