博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OC KVC总结
阅读量:5874 次
发布时间:2019-06-19

本文共 5890 字,大约阅读时间需要 19 分钟。

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

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

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

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

  Student类:

1 //.h文件 2 #import 
3 @class Book; 4 @interface Student : NSObject 5 6 @property (nonatomic, assign) int age; 7 8 @property (nonatomic, copy) NSString* name; 9 10 11 @property (nonatomic, strong) Book* book;12 13 @property (nonatomic, strong) NSArray* books;14 @end15 16 //.m文件17 18 #import "Student.h"19 #import "Book.h"20 @implementation Student21 22 23 - (NSString *) description24 {25 return [NSString stringWithFormat:@"%@ %d", _name, _age];26 }27 @end

  Book类:

  

1 //.h文件 2 #import 
3 4 @interface Book : NSObject 5 6 7 @property (nonatomic, assign) int price; 8 @end 9 10 //.m文件11 #import "Book.h"12 13 @implementation Book14 15 @end

  主函数:

  

1 #import 
2 #import "Student.h" 3 #import "Book.h" 4 5 6 //1.设值 取值 7 void fun1() 8 { 9 NSLog(@"==============fun1=============="); 10 Student *stu = [[Student alloc] init]; 11 12 [stu setValue:@"hehe" forKey:@"name"]; 13 [stu setValue:@10 forKey:@"age"]; 14 15 NSLog(@"%@", stu); 16 17 NSLog(@"name : %@", [stu valueForKey:@"name"]); 18 } 19 20 //2.批量获取属性值 21 void fun2() 22 { 23 NSLog(@"==============fun2=============="); 24 Student *stu = [[Student alloc] init]; 25 26 [stu setValue:@"hehe" forKey:@"name"]; 27 [stu setValue:@10 forKey:@"age"]; 28 29 NSArray *arr = @[@"name", @"age"]; 30 31 NSDictionary *dict = [stu dictionaryWithValuesForKeys:arr]; 32 33 NSLog(@"%@", dict); 34 } 35 36 //3.批量设置属性值(通过字典批量赋值) 37 void fun3() 38 { 39 NSLog(@"==============fun3=============="); 40 Student *stu = [[Student alloc] init]; 41 42 NSDictionary *dict = @{
@"age": @12, @"name" : @"悟空"}; 43 44 [stu setValuesForKeysWithDictionary:dict]; 45 46 NSLog(@"%@", stu); 47 } 48 49 50 //4.通过键路径(key path)访问(使用valueForKeyPath) 51 void fun4() 52 { 53 NSLog(@"==============fun4=============="); 54 Student *stu = [[Student alloc] init]; 55 Book *book = [[Book alloc] init]; 56 stu.book = book; 57 //设置值 58 [stu setValue:@12 forKeyPath:@"book.price"]; 59 //获取值 60 NSNumber *price = [stu valueForKeyPath:@"book.price"]; 61 62 NSLog(@"price: %@", price); 63 } 64 65 //5.对数组进行整体操作 66 void fun5() 67 { 68 NSLog(@"==============fun5=============="); 69 Student *stu = [[Student alloc] init]; 70 71 Book *book = [[Book alloc] init]; 72 book.price = 12; 73 Book *book1 = [[Book alloc] init]; 74 book1.price = 10; 75 Book *book2 = [[Book alloc] init]; 76 book2.price = 12; 77 78 NSArray *arr = @[book, book1, book2]; 79 80 stu.books = arr; 81 //获取值 82 // NSArray *prices = [stu valueForKeyPath:@"books.price"]; 83 NSArray *prices = [stu.books valueForKeyPath:@"price"]; 84 NSLog(@"prices: %@", prices); 85 } 86 87 //6.键路径的运算符(数组元素求和) 88 void fun6() 89 { 90 NSLog(@"==============fun6=============="); 91 Student *stu = [[Student alloc] init]; 92 93 Book *book = [[Book alloc] init]; 94 book.price = 12; 95 Book *book1 = [[Book alloc] init]; 96 book1.price = 10; 97 Book *book2 = [[Book alloc] init]; 98 book2.price = 12; 99 100 NSArray *arr = @[book, book1, book2];101 102 stu.books = arr;103 //获取值104 NSNumber *sum = [stu valueForKeyPath:@"books.@sum.price"];105 NSLog(@"prices: %@", sum);106 }107 108 //7.键路径的运算符(数组总数count)109 void fun7()110 {111 NSLog(@"==============fun7==============");112 Student *stu = [[Student alloc] init];113 114 Book *book = [[Book alloc] init];115 book.price = 12;116 Book *book1 = [[Book alloc] init];117 book1.price = 10;118 Book *book2 = [[Book alloc] init];119 book2.price = 12;120 121 NSArray *arr = @[book, book1, book2];122 123 stu.books = arr;124 //获取值125 NSNumber *sum = [stu valueForKeyPath:@"books.@count.price"];126 NSLog(@"num: %@", sum);127 }128 129 //8.键路径的运算符(获取数组不同元素)130 void fun8()131 {132 NSLog(@"==============fun8==============");133 Student *stu = [[Student alloc] init];134 135 Book *book = [[Book alloc] init];136 book.price = 12;137 Book *book1 = [[Book alloc] init];138 book1.price = 10;139 Book *book2 = [[Book alloc] init];140 book2.price = 12;141 142 NSArray *arr = @[book, book1, book2];143 144 stu.books = arr;145 //获取值146 NSArray *array = [stu valueForKeyPath:@"books.@distinctUnionOfObjects.price"];147 NSLog(@"array: %@", array);148 }149 150 //9.求数组元素中得最大值、最小值、平均值151 void fun9()152 {153 NSLog(@"==============fun9==============");154 Student *stu = [[Student alloc] init];155 156 Book *book = [[Book alloc] init];157 book.price = 12;158 Book *book1 = [[Book alloc] init];159 book1.price = 10;160 Book *book2 = [[Book alloc] init];161 book2.price = 12;162 163 NSArray *arr = @[book, book1, book2];164 165 stu.books = arr;166 //获取值167 NSNumber *max = [stu valueForKeyPath:@"books.@max.price"];168 NSNumber *min = [stu valueForKeyPath:@"books.@min.price"];169 NSNumber *average = [stu valueForKeyPath:@"books.@avg.price"];170 NSLog(@"max: %@", max);171 NSLog(@"min: %@", min);172 NSLog(@"avg: %@", average);173 }174 175 176 int main(int argc, const char * argv[])177 {178 179 @autoreleasepool {180 fun1();181 fun2();182 fun3();183 fun4();184 fun5();185 fun6();186 fun7();187 fun8();188 fun9();189 }190 return 0;191 }

 

 

  

转载于:https://www.cnblogs.com/pretty-guy/p/4056016.html

你可能感兴趣的文章
做socket保持长连接的一些心得
查看>>
Lumia 800 7.10.8858.136
查看>>
The disk cannot be added to Cluster Shared Volumes because it does not have any suitable partitions
查看>>
C#: 鼠标和键盘的输入事件
查看>>
Google Code 创建开源项目
查看>>
Script:收集数据库中用户的角色和表空间等信息
查看>>
Java连接MySql
查看>>
gdb调试命令
查看>>
ASP.NET 网站部署 Part.1(安装IIS、复制文件部署网站)
查看>>
怎样才能当上项目经理?(转)
查看>>
消息、接收-关于Erlang Socket的三种消息接收模式-by小雨
查看>>
文件配置IBM Ration Rhapsody 软件的环境配置
查看>>
蛇形填数
查看>>
PostgreSQL在何处处理 sql查询之五十九
查看>>
T-SQL查询进阶--深入浅出视图
查看>>
iOS 如何创建单例对象
查看>>
skype msnLite 静态路由
查看>>
Hibernate原理
查看>>
SlidingMenu+Fragment实现当前最流行的侧滑
查看>>
我是如何写作一本软件+哲学式的书籍的(下)
查看>>