在iOS开发中,我们一般使用set方法或者点语法来修改对象的属性值,比如说 stu.age = 9 与 [stu setAge:9]。

  KVC(key value coding)键值编码,这是一种间接修改对象属性值的方法。实现方法就是通过用字符串来描述要修改的属性。基本的操作方法有 setValue:forKey: 和 valueForKey,以字符串的形式发送对象

  特别提醒,使用KVC中所有的value都必须是对象。

在此以Student类和 Book类作为例子来总结

  Student类:

 //.h文件
#import <Foundation/Foundation.h>
@class Book;
@interface Student : NSObject @property (nonatomic, assign) int age; @property (nonatomic, copy) NSString* name; @property (nonatomic, strong) Book* book; @property (nonatomic, strong) NSArray* books;
@end //.m文件 #import "Student.h"
#import "Book.h"
@implementation Student - (NSString *) description
{
return [NSString stringWithFormat:@"%@ %d", _name, _age];
}
@end

  Book类:

  

 //.h文件
#import <Foundation/Foundation.h> @interface Book : NSObject @property (nonatomic, assign) int price;
@end //.m文件
#import "Book.h" @implementation Book @end

  主函数:

  

 #import <Foundation/Foundation.h>
#import "Student.h"
#import "Book.h" //1.设值 取值
void fun1()
{
NSLog(@"==============fun1==============");
Student *stu = [[Student alloc] init]; [stu setValue:@"hehe" forKey:@"name"];
[stu setValue:@ forKey:@"age"]; NSLog(@"%@", stu); NSLog(@"name : %@", [stu valueForKey:@"name"]);
} //2.批量获取属性值
void fun2()
{
NSLog(@"==============fun2==============");
Student *stu = [[Student alloc] init]; [stu setValue:@"hehe" forKey:@"name"];
[stu setValue:@ forKey:@"age"]; NSArray *arr = @[@"name", @"age"]; NSDictionary *dict = [stu dictionaryWithValuesForKeys:arr]; NSLog(@"%@", dict);
} //3.批量设置属性值(通过字典批量赋值)
void fun3()
{
NSLog(@"==============fun3==============");
Student *stu = [[Student alloc] init]; NSDictionary *dict = @{@"age": @, @"name" : @"悟空"}; [stu setValuesForKeysWithDictionary:dict]; NSLog(@"%@", stu);
} //4.通过键路径(key path)访问(使用valueForKeyPath)
void fun4()
{
NSLog(@"==============fun4==============");
Student *stu = [[Student alloc] init];
Book *book = [[Book alloc] init];
stu.book = book;
//设置值
[stu setValue:@ forKeyPath:@"book.price"];
//获取值
NSNumber *price = [stu valueForKeyPath:@"book.price"]; NSLog(@"price: %@", price);
} //5.对数组进行整体操作
void fun5()
{
NSLog(@"==============fun5==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
// NSArray *prices = [stu valueForKeyPath:@"books.price"];
NSArray *prices = [stu.books valueForKeyPath:@"price"];
NSLog(@"prices: %@", prices);
} //6.键路径的运算符(数组元素求和)
void fun6()
{
NSLog(@"==============fun6==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
NSNumber *sum = [stu valueForKeyPath:@"books.@sum.price"];
NSLog(@"prices: %@", sum);
} //7.键路径的运算符(数组总数count)
void fun7()
{
NSLog(@"==============fun7==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
NSNumber *sum = [stu valueForKeyPath:@"books.@count.price"];
NSLog(@"num: %@", sum);
} //8.键路径的运算符(获取数组不同元素)
void fun8()
{
NSLog(@"==============fun8==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
NSArray *array = [stu valueForKeyPath:@"books.@distinctUnionOfObjects.price"];
NSLog(@"array: %@", array);
} //9.求数组元素中得最大值、最小值、平均值
void fun9()
{
NSLog(@"==============fun9==============");
Student *stu = [[Student alloc] init]; Book *book = [[Book alloc] init];
book.price = ;
Book *book1 = [[Book alloc] init];
book1.price = ;
Book *book2 = [[Book alloc] init];
book2.price = ; NSArray *arr = @[book, book1, book2]; stu.books = arr;
//获取值
NSNumber *max = [stu valueForKeyPath:@"books.@max.price"];
NSNumber *min = [stu valueForKeyPath:@"books.@min.price"];
NSNumber *average = [stu valueForKeyPath:@"books.@avg.price"];
NSLog(@"max: %@", max);
NSLog(@"min: %@", min);
NSLog(@"avg: %@", average);
} int main(int argc, const char * argv[])
{ @autoreleasepool {
fun1();
fun2();
fun3();
fun4();
fun5();
fun6();
fun7();
fun8();
fun9();
}
return ;
}

  

OC KVC总结的更多相关文章

  1. OC KVC

    OC KVC KVC 全称 key valued coding 键值编码 在说KVC之前应该简单的了解一下反射机制 反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法. 对于任意 ...

  2. oc kvc的模式:匹配搜索模式(模式匹配)、装包解包

    按照一定规则使用匹配模式在目标空间进行搜索,然后执行相应操作: 运行时系统将kvc的运行机制解释为模式匹配,将值的兼容性问题解释为装包解包问题 一.模式匹配 The default implement ...

  3. iOS 头条一面 面试题

    1.如何高效的切圆角? 切圆角共有以下三种方案: cornerRadius + masksToBounds:适用于单个视图或视图不在列表上且量级较小的情况,会导致离屏渲染. CAShapeLayer+ ...

  4. OC:属性、点语法、KVC

    //属性的属性 属性定义在一个 .h文件里,在这个.h文件里可以定义实例变量(就是这个类的特征),也可以通过   @protery(属性约束关键字) 属性名字类型 属性名 来定义一些属性,在prope ...

  5. KVC 和 OC字典

    KVC(键值编码)和OC 字典很相似,都是键值存储.但是OC 字典比较灵活,它是一种映射. [dict setObject:<#(id)#> forKey:<#(id<NSCo ...

  6. QF——OC中的KVC,KVO

    KVC: (Key Value Coding) 键值编码 所谓KVC,其实就是不通过set和get方法访问对象属性,而是通过属性名字符串动态的去读取属性.KVC其实也是OC反射机制的一种运用. 之所以 ...

  7. 设计模式之观察者模式(关于OC中的KVO\KVC\NSNotification)

    学习了这么久的设计模式方面的知识,最大的感触就是,设计模式不能脱离语言特性.近段时间所看的两本书籍,<大话设计模式>里面的代码是C#写的,有一些设计模式实现起来也是采用了C#的语言特性(C ...

  8. OC学习篇之---KVC和KVO操作

    前一篇文章我们介绍了OC中最常用的文件操作:http://blog.csdn.net/jiangwei0910410003/article/details/41875015,那么今天来看一下OC中的一 ...

  9. [转] iOS (OC) 中 KVC 与 KVO 理解

    转自: http://magicalboy.com/kvc_and_kvo/ KVC 与 KVO 是 Objective C 的关键概念,个人认为必须理解的东西,下面是实例讲解. Key-Value ...

随机推荐

  1. 今天在发布IIS站点的时候遇到了一些问题

    1.HTTP 错误 500.23 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设置. 分析:一般5XX的错误都是服务器的问题,这里把应用程序池 ...

  2. 米兰站热卖:奢侈品电商困局已破?-搜狐IT

    米兰站热卖:奢侈品电商困局已破?-搜狐IT 米兰站热卖:奢侈品电商困局已破?

  3. css引入讲解及media

    引用Css的几种方式: 一.@import <style type="text/css" media="screen"> @import url(& ...

  4. Jquer学习之jQuery(function(){})与(function(){})(jQuery)之间的区别

    Jquery是优秀的Javascrīpt框架.我们现在来讨论下在 Jquery 中两个页面载入后执行的函数. $(document).ready(function(){ // 在这里写你的代码... ...

  5. 使用x manager 连接Linux桌面

    /usr/bin/xterm -ls -display $DISPLAY 需要安装xterm 服务

  6. 使用分析函数实现Oracle 10G提供的CONNECT_BY_ISLEAF和CONNECT_BY_ROOT的功能(转载)

    文章转载至:http://blog.csdn.net/wzy0623/article/details/1644049 如果,有侵犯您权益的地方,烦请及时的告知我,我会即刻停止侵权行为 Oracle 1 ...

  7. mysql批量上传数据

    private object BlubckMysql(List<xiaoyao_blogs_pictureModel> list, string connect) { var sqllis ...

  8. HTML5 调用手机相册和摄像头的方法并上传微信下测试通过

    <input type="file" capture="camera" accept="image/*" id="camer ...

  9. PHP -- 添加注释

    PHP支持3种风格的注释 1.C++风格(//)的注释 这种注释不能出现?>标记,如果开启short_open和asp_tag设置,>和%>同样不能出现在注释中 <?php e ...

  10. Web存储(Web Storage)介绍

    Web存储即在客户端存储数据. 在没有Web Storage之前,是通过cookie来在客户端存储数据的.但是由于 浏览器能存cookie数比较少.如IE8,Firefox,opera每个域可以保存的 ...