OC KVC总结

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

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

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

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

  Student类:

 1 //.h文件
 2 #import <Foundation/Foundation.h>
 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 @end
15 
16 //.m文件
17 
18 #import "Student.h"
19 #import "Book.h"
20 @implementation Student
21 
22 
23 - (NSString *) description
24 {
25     return  [NSString stringWithFormat:@"%@ %d", _name, _age];
26 }
27 @end

  Book类:

  

 1 //.h文件
 2 #import <Foundation/Foundation.h>
 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 Book
14 
15 @end

  主函数:

  

  1 #import <Foundation/Foundation.h>
  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

上一篇:Jhipster创建实体


下一篇:iOS面试题