在目标c中实现键值编码 [英] Implementing Key Value coding in Objective c

查看:56
本文介绍了在目标c中实现键值编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在时间在程序中实现键值编码,因此我对此不太满意.我有一个控制器类,其中在起始文档中实现了解析方法,我已经初始化了对象和键.通用类的所有数据成员都是id类型.

I am time implementing the key value coding in my program so i am not much comfortable with it . I am having the controller class in which the parsing methods are implemented in start document i have initialize the object and keys. All data members of class Universal are type id.

            univ=[[Universal alloc] init];
            [univ setValue:univ.datamember1 forKey:@"1"];
            [univ setValue:univ.datamember2 forKey:@"2"];
            [univ setValue:univ.datamember3 forKey:@"3"];
            [univ setValue:univ.datamember4 forKey:@"4"];
            [univ setValue:univ.datamember5 forKey:@"5"];

我正在使用end element方法设置数据成员的值:

I am setting the value on data member in end element method :

         id temp1=[univ valueForKey:@"1"];
        temp1=strVal;
        [resultArray addObject:univ];

并且我收到此代码的以下异常:

and i am getting the following exception for this code:

'NSUnknownKeyException', reason: '[<Universal 0x6e994d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key 1

这是什么原因.是否缺少某些东西或一切都出错了?

what is the reason for this. Is there missing something or everything gone wrong??

推荐答案

在大多数情况下,您无需实现键值编码.相反,您可以创建类来遵守各种约定,然后键值编码会自动对其进行工作.

For the most part, you don't have to implement Key-Value Coding. Instead, you create your classes to comply with various conventions and then Key-Value Coding works for it automatically.

这些约定是关于如何命名方法以使属性可以通过KVC访问的

The conventions are about how you name methods in order to make a property accessible via KVC.

您的代码:

        univ=[[Universal alloc] init];
        [univ setValue:univ.datamember1 forKey:@"1"];
        [univ setValue:univ.datamember2 forKey:@"2"];
        [univ setValue:univ.datamember3 forKey:@"3"];
        [univ setValue:univ.datamember4 forKey:@"4"];
        [univ setValue:univ.datamember5 forKey:@"5"];

很奇怪.您想在这里做什么?这段代码看起来就像试图从univ对象的其他属性中设置univ对象的属性一样.那将是多余的.

is strange. What are you trying to do here? This code looks like it's trying to set properties on the univ object from other properties of the univ object. That would be redundant.

我怀疑您认为您正在此处建立同义词或别名-您正在将键"1"映射到属性datamember1.那是不对的. -setValue:forKey:方法用于实际设置属性的值.这非常类似于调用setter方法.实际上,它通常会导致调用setter方法.

I suspect you think that you're establishing synonyms or aliases here – that you're making key "1" map to the property datamember1. That's not correct. The -setValue:forKey: method is for actually setting a property's value. It's very much like calling a setter method; in fact, it usually does result in calling a setter method.

因此,您编写的内容非常类似于以下内容:

So, what you've written is very much like the following:

        univ=[[Universal alloc] init];
        [univ set1:univ.datamember1];
        [univ set2:univ.datamember2];
        [univ set3:univ.datamember3];
        [univ set4:univ.datamember4];
        [univ set5:univ.datamember5];

我怀疑这是您要执行的操作.它可能会帮助您以非KVC术语解释您要尝试执行的操作.您尝试更改哪些属性(如果有)?您想为它们分配什么值?您将如何仅使用普通的setter和getter来做到这一点?

I doubt that's anything that you meant to do. It might help you to explain what you're trying to do in non-KVC terms. What properties are you trying change (if you are)? What values would you like to assign to them? How would you do that using just ordinary setters and getters?

稍后,您发布了以下代码:

Later, you posted this code:

    id temp1=[univ valueForKey:@"1"];
    temp1=strVal;
    [resultArray addObject:univ];

再次,这很奇怪而且令人困惑.我怀疑您正在尝试更改univ上的属性值,但这不是代码可以完成的,即使它可以工作也是如此.第一行尝试获取univ对象的属性的值,该属性的名称为"1".这不是属性的有效名称.无论如何,它将检索到的值存储到局部变量temp1中.第二行只是丢弃第一行的结果,并将另一个值存储到本地temp1变量中. temp1变量独立于univ对象,即使不久前它存储从univ对象检索的结果.更改temp1不能更改univ(尽管通过消息传递temp1指向的对象可以更改该对象,并且它也可以由univ指向).

Again, this is strange and confusing. I suspect you're trying to change a property value on univ, but that's not what that code accomplishes, even if it could be made to work. The first line attempts to get the value of a property of the univ object, where the property's name is "1". That's not a valid name for a property. Anyway, it stores the retrieved value into a local variable temp1. The second line simply throws away the result of the first line and stores a different value into the local temp1 variable. The temp1 variable is independent of the univ object, even though a moment ago it was storing a result retrieved from the univ object. Changing temp1 can't change univ (although messaging the object pointed to by temp1 could change that object and it might also be pointed to by univ).

在我看来,您尚未准备好使用键值编码.您需要对基础知识有更好的了解.另外,几乎不需要将键值编码与静态的,编译时已知的键一起使用.键值编码用于动态访问对象的属性,当您在编译时不知道该属性的名称,但是在运行时将其命名为作为数据.

It seems to me that you aren't yet ready to be using Key-Value Coding. You need a better understanding of the basics. Also, it's almost never necessary to use Key-Value Coding with a static, known-at-compile-time key. Key-Value Coding is for dynamic access to properties of an object when you don't know the name of that property at compile time but you'll have the name as data at run time.

这篇关于在目标c中实现键值编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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