1 //
2 // Model.m
3 // KVC熟悉与加强
4 //
5 // Created by ma c on 16/5/16.
6 // Copyright © 2016年 彭盛凇. All rights reserved.
7 //
8
9 #import "Model.h"
10
11 @implementation Model
12
13 - (NSString *)description
14 {
15 return [NSString stringWithFormat:@"model.name = %@, mdoel.height = %@", _name, @(_height)];
16 }
17
18 - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
19
20 NSLog(@"key = %@没有找到",key);
21
22 }
23
24 + (NSArray *)loadData {
25
26 //获取plist文件路径
27 NSString *path = @"/Users/mac/Desktop/KVC熟悉与加强/KVC熟悉与加强/CoderPSS.plist";
28
29 //获取plist文件中的数组
30 NSArray *array = [NSArray arrayWithContentsOfFile:path];
31
32 //初始化可变数组
33 NSMutableArray *dataList = [NSMutableArray array];
34
35 //使用字典遍历plist中的数组
36 for (NSDictionary *dict in array) {
37
38 //初始化model类
39 Model *model = [[Model alloc] init];
40
41 //使用KVC(setValuesForKeysWithDictionary)方法解析数据
42 [model setValuesForKeysWithDictionary:dict];
43
44 //将model类加到可变数组
45 [dataList addObject:model];
46 }
47
48 //返回可变数组
49 return dataList;
50 }
51
52 @end