键值编码的基本概念

      1:键值编码是一个用于简介访问对象属性的机制,使用该机制不需要调用存取方法和变量实例

就可以访问对象属性。

      2:键值编码方法在Objective-C非正式协(类别)NSKeyValueCoding中被声明,默认的实现方法

由NSObject提供。

      3:键值编码支持带有对象指的属性,同时也支持数值类型和结构。非对象参数和返回类型会被

识别并自动封装/解封。

设置与访问:

       键值编码洪的基本调用包括-valueForKey:和setValue:forKey:这两个方法,他们以字符串的形式

向对象发送消息,字符串是我们关注属性的关键,看下面的一个设置和访问的例子:

        Person *person=[[Person alloc]init];

        NSMutableString *name=[[NSMutableString alloc]initWithString:@"张三"];

        [person setValue:name forKey:@"name"];

        NSLog(@"张三 name :%@",[person valueForKey:@"name"]);

首先去检查是否存在setter,getter方法,如果不存在,它将在内部查找名_key或key的实例变量。如果


还是没有那就会报错误。通过KVC可以获取不存在getter方法的对象值,无需通过对象指针直接访问。

【注意】:当我们通过setValue:forKey:设置对象的值,或者valueForKey:来获取对象的值时,如果对

象的实例变量为基本数据类型时(例如:char,int,float,BOOL),我们需要对这些数据类型进行封装。


路径和一对多的关系

路径:

      除了上面通过键值进行设置和访问意外,键值还支持指定路径,像文件一样。用"."号隔开。实例如下:

        Person *person=[[Person alloc]init];

        Auther *auther=[[Auther alloc]init];

        [auther setValue:@"iOS开发" forKey:@"title"];

        NSMutableString *name=[[NSMutableString alloc]initWithString:@"张三"];

        [person setValue:name forKey:@"name"];

        [person setValue:auther forKey:@"auther"];

        NSLog(@"name :%@",[person valueForKey:@"name"]);

        NSString *title=[person valueForKeyPath:@"auther.title"];

        NSLog(@"title :%@",title);


一对多关系:

        如果向NSArray请求一个键值,它实际上回查询数组中的每个对象来查找这个键值,然后将查询结果

打包到另一个数组中并返回给你。实例如下:

             //一对多关系

        NSMutableArray *persons=[NSMutableArray arrayWithCapacity:3];

        for(int i=0;i<3;i++){

            Person *peson1=[[Person alloc]init];

            NSString *name=[NSString stringWithFormat:@"person_%d",i];

            [peson1 setValue:name forKey:@"name"];

            [persons addObject:peson1];

            [peson1 release];

        }

       [person setValue:persons forKey:@"rePerson"];

        NSArray *nameArray= [person valueForKeyPath:@"rePerson.name"];

         NSLog(@"%@",nameArray);



KVC的简单运算:

      此外,还可以应用一些字符做简单运算。sum,min,max,avg,count

<span class="pln" style="word-wrap:break-word">        </span><span class="com" style="color:#8800;word-wrap:break-word;">//属性相加</span><span class="pln" style="word-wrap:break-word">
        </span><span class="typ" style="color:#66066;word-wrap:break-word;">NSString</span><span class="pln" style="word-wrap:break-word"> </span><span class="pun" style="color:#66660;word-wrap:break-word;">*</span><span class="pln" style="word-wrap:break-word">sum</span><span class="pun" style="color:#66660;word-wrap:break-word;">=</span><span class="pln" style="word-wrap:break-word"> </span><span class="pun" style="color:#66660;word-wrap:break-word;">[</span><span class="pln" style="word-wrap:break-word">person valueForKeyPath</span><span class="pun" style="color:#66660;word-wrap:break-word;">:@</span><span class="str" style="color:#0880;word-wrap:break-word;">"rePerson.@sum.age"</span><span class="pun" style="color:#66660;word-wrap:break-word;">];</span><span class="pln" style="word-wrap:break-word">
        </span><span class="typ" style="color:#66066;word-wrap:break-word;">NSLog</span><span class="pun" style="color:#66660;word-wrap:break-word;">(@</span><span class="str" style="color:#0880;word-wrap:break-word;">"sum = %@"</span><span class="pun" style="color:#66660;word-wrap:break-word;">,</span><span class="pln" style="word-wrap:break-word">sum</span><span class="pun" style="color:#66660;word-wrap:break-word;">);</span><span class="pln" style="word-wrap:break-word">
        </span><span class="com" style="color:#8800;word-wrap:break-word;">//数量</span><span class="pln" style="word-wrap:break-word">
        </span><span class="typ" style="color:#66066;word-wrap:break-word;">NSString</span><span class="pln" style="word-wrap:break-word"> </span><span class="pun" style="color:#66660;word-wrap:break-word;">*</span><span class="pln" style="word-wrap:break-word">count</span><span class="pun" style="color:#66660;word-wrap:break-word;">=</span><span class="pln" style="word-wrap:break-word"> </span><span class="pun" style="color:#66660;word-wrap:break-word;">[</span><span class="pln" style="word-wrap:break-word">person valueForKeyPath</span><span class="pun" style="color:#66660;word-wrap:break-word;">:@</span><span class="str" style="color:#0880;word-wrap:break-word;">"rePerson.@count.age"</span><span class="pun" style="color:#66660;word-wrap:break-word;">];</span><span class="pln" style="word-wrap:break-word">
        </span><span class="typ" style="color:#66066;word-wrap:break-word;">NSLog</span><span class="pun" style="color:#66660;word-wrap:break-word;">(@</span><span class="str" style="color:#0880;word-wrap:break-word;">"count = %@"</span><span class="pun" style="color:#66660;word-wrap:break-word;">,</span><span class="pln" style="word-wrap:break-word">count</span><span class="pun" style="color:#66660;word-wrap:break-word;">);</span><span class="pln" style="word-wrap:break-word">
        </span><span class="com" style="color:#8800;word-wrap:break-word;">//最大值</span><span class="pln" style="word-wrap:break-word">
        </span><span class="typ" style="color:#66066;word-wrap:break-word;">NSString</span><span class="pln" style="word-wrap:break-word"> </span><span class="pun" style="color:#66660;word-wrap:break-word;">*</span><span class="pln" style="word-wrap:break-word">max</span><span class="pun" style="color:#66660;word-wrap:break-word;">=</span><span class="pln" style="word-wrap:break-word"> </span><span class="pun" style="color:#66660;word-wrap:break-word;">[</span><span class="pln" style="word-wrap:break-word">person valueForKeyPath</span><span class="pun" style="color:#66660;word-wrap:break-word;">:@</span><span class="str" style="color:#0880;word-wrap:break-word;">"rePerson.@max.age"</span><span class="pun" style="color:#66660;word-wrap:break-word;">];</span><span class="pln" style="word-wrap:break-word">
        </span><span class="typ" style="color:#66066;word-wrap:break-word;">NSLog</span><span class="pun" style="color:#66660;word-wrap:break-word;">(@</span><span class="str" style="color:#0880;word-wrap:break-word;">"max = %@"</span><span class="pun" style="color:#66660;word-wrap:break-word;">,</span><span class="pln" style="word-wrap:break-word">max</span><span class="pun" style="color:#66660;word-wrap:break-word;">);</span><span class="pln" style="word-wrap:break-word">
        </span><span class="com" style="color:#8800;word-wrap:break-word;">//最小值</span><span class="pln" style="word-wrap:break-word">
        </span><span class="typ" style="color:#66066;word-wrap:break-word;">NSString</span><span class="pln" style="word-wrap:break-word"> </span><span class="pun" style="color:#66660;word-wrap:break-word;">*</span><span class="pln" style="word-wrap:break-word">min</span><span class="pun" style="color:#66660;word-wrap:break-word;">=</span><span class="pln" style="word-wrap:break-word"> </span><span class="pun" style="color:#66660;word-wrap:break-word;">[</span><span class="pln" style="word-wrap:break-word">person valueForKeyPath</span><span class="pun" style="color:#66660;word-wrap:break-word;">:@</span><span class="str" style="color:#0880;word-wrap:break-word;">"rePerson.@min.age"</span><span class="pun" style="color:#66660;word-wrap:break-word;">];</span><span class="pln" style="word-wrap:break-word">
        </span><span class="typ" style="color:#66066;word-wrap:break-word;">NSLog</span><span class="pun" style="color:#66660;word-wrap:break-word;">(@</span><span class="str" style="color:#0880;word-wrap:break-word;">"min = %@"</span><span class="pun" style="color:#66660;word-wrap:break-word;">,</span><span class="pln" style="word-wrap:break-word">min</span><span class="pun" style="color:#66660;word-wrap:break-word;">);</span><span class="pln" style="word-wrap:break-word">
        </span><span class="com" style="color:#8800;word-wrap:break-word;">//平均值</span><span class="pln" style="word-wrap:break-word">
        </span><span class="typ" style="color:#66066;word-wrap:break-word;">NSString</span><span class="pln" style="word-wrap:break-word"> </span><span class="pun" style="color:#66660;word-wrap:break-word;">*</span><span class="pln" style="word-wrap:break-word">avg</span><span class="pun" style="color:#66660;word-wrap:break-word;">=</span><span class="pln" style="word-wrap:break-word"> </span><span class="pun" style="color:#66660;word-wrap:break-word;">[</span><span class="pln" style="word-wrap:break-word">person valueForKeyPath</span><span class="pun" style="color:#66660;word-wrap:break-word;">:@</span><span class="str" style="color:#0880;word-wrap:break-word;">"rePerson.@avg.age"</span><span class="pun" style="color:#66660;word-wrap:break-word;">];</span><span class="pln" style="word-wrap:break-word">
        </span><span class="typ" style="color:#66066;word-wrap:break-word;">NSLog</span><span class="pun" style="color:#66660;word-wrap:break-word;">(@</span><span class="str" style="color:#0880;word-wrap:break-word;">"avg = %@"</span><span class="pun" style="color:#66660;word-wrap:break-word;">,</span><span class="pln" style="word-wrap:break-word">avg</span><span class="pun" style="color:#66660;word-wrap:break-word;">);</span>