一、使用KVC实现字典转模型
1. 加载plist文件,将plist文件转为字典
2. 字典转模型
注意: 使用KVC的 setValuesForKeysWithDictionary: 方法为模型属性赋值时, 必须保证模型的属性名和plist中的属性名完全相同,否则会报 setValue:forUndefinedKey: 错误.
当plist文件中有很多属性,我们在模型中的属性名也要和plist文件中的属性名一一对应,这样写模型属性的时候会非常麻烦,因为模型中的属性名和plist文件中的key有联系,因此我们给 NSDictionary写一个自动生成属性的分类.
这样做的好处: 1. 防止模型的属性和plist文件中的key名写错,然后报错
3. KVC原理
setValuesForKeysWithDictionary:方法底层①②:
① 遍历字典中所有的key,去模型中查找有没有对应的属性
② 去模型中查找有没有对应的属性
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull value, BOOL * _Nonnull stop) {
//2. 去模型中查找有没有对应的属性 KVC
// [item setValue:@"来自即可笔记" forKey:@"sourse"];
[item setValue:value forKey:key];
}];
setValue:forKey:方法底层做的事情
[item setValue:@"笔记" forKey:@"sourse"];
① 首先去模型中查找有没有setSourse,找到,直接调用[self setSourse:@"笔记"];
② 去模型中查找有没有source属性,有,直接访问属性赋值 sourse = value
③ 去模型中查找有没有_source属性,有,直接访问属性赋值 _sourse = value ④ 找不到,直接报错 setValue:forUndefinedKey: 报找不到的错误
注意: 有时候模型中只保存最重要的数据,导致模型的属性和字典不能一一对应.此时: 可以采用
①最原始的做法 item.source = dict[@"source"]; 根据字典的key对应的value赋值给模型属性(太繁琐)
②使用第三方框架MJExtension 字典转模型. 框架底层runtime : 把模型中所有的属性都遍历出来
③还是使用KVC的方式, 当报setValue:forUndefinedKey:错误,表面找不到属性, 只需要从写系统的setValue:forUndefinedKey:方法, 方法体中什么都不做即可!
NSDictionary+Property 文件
#import "NSDictionary+Property.h"
@implementation NSDictionary (Property)
// isKindOfClass: 判断是否是当前类或子类
// 生成属性代码 => 根据字典中的所有key
- (void)createPropertyCode
{
NSMutableString *codes = [NSMutableString string];
// 遍历字典
[self enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull value, BOOL * _Nonnull stop) {
NSLog(@"%@",[value class]);
NSString *code;
// 根据value来判断是什么类型
if ([value isKindOfClass:[NSString class]]) {
code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSString *%@;",key];
}else if ([value isKindOfClass:[NSDictionary class]]){
code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSDictionary *%@;",key];
}else if([value isKindOfClass:NSClassFromString(@"__NSCFBoolean")]){
code = [NSString stringWithFormat:@"@property (nonatomic, assign) BOOL %@;",key];
}else if ([value isKindOfClass:[NSNumber class]]){
code = [NSString stringWithFormat:@"@property (nonatomic, assign) NSInteger %@;",key];
}else if ([value isKindOfClass:[NSArray class]]){
code = [NSString stringWithFormat:@"@property (nonatomic, strong) NSArray *%@;",key];
}
[codes appendFormat:@"\n%@\n",code];
}];
NSLog(@"%@",codes);
}
@end
当一个字典来调用
createPropertyCode 方法时,就会自动生成该plist文件中所有key类型的属性.
ViewController文件
#import "ViewController.h"
#import "NSDictionary+Property.h"
#import "ZYStatusItem.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1. 加载plist文件
// 获取文件全路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"status.plist" ofType:nil];
// 文件全路径
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
// 设置模型,创建属性代码 ==> dict
//[dict createPropertyCode];
// 字典转模型
ZYStatusItem *item = [ZYStatusItem itemWithDict:dict];
// NSLog(@"%@",item.source);
NSLog(@"%@",item.created_at);
}
@end
ZYStatusItem文件
#import
@interface ZYStatusItem : NSObject
//@property (nonatomic, strong) NSString *source;
@property (nonatomic, assign) NSInteger reposts_count;
@property (nonatomic, strong) NSArray *pic_urls;
@property (nonatomic, strong) NSString *created_at;
@property (nonatomic, assign) BOOL isA;
@property (nonatomic, assign) NSInteger attitudes_count;
@property (nonatomic, strong) NSString *idstr;
@property (nonatomic, strong) NSString *text;
@property (nonatomic, assign) NSInteger comments_count;
@property (nonatomic, strong) NSDictionary *user;
+ (instancetype)itemWithDict:(NSDictionary *)dict;
@end
#import "ZYStatusItem.h"
@implementation ZYStatusItem
// 模型只保存最重要的数据,导致模型的属性和字典不能一一对应
+ (instancetype)itemWithDict:(NSDictionary *)dict
{
ZYStatusItem *item = [[self alloc] init];
// 拿到每个模型的属性,从字典中取出对应的value,给模型赋值
// item.source = dict[@"source"];
// 使用上面这种方法太繁琐,如果模型有n个属性,太麻烦.使用MJExtension
// MJExtension:字典转模型 runtime实现:把模型中的所有属性都遍历出来
// KVC
// [item setValuesForKeysWithDictionary:dict];
// KVC原理:
//1. 遍历字典中所有的key,去模型中查找有没有对应的属性
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull value, BOOL * _Nonnull stop) {
//2. 去模型中查找有没有对应的属性 KVC
// [item setValue:@"来自即可笔记" forKey:@"sourse"];
[item setValue:value forKey:key];
}];
/*
[item setValue:@"来自即可笔记" forKey:@"sourse"];
1. 首先去模型中查找有没有setSourse,找到,直接调用赋值[self setSource:@"来自即刻笔记"]
2. 去模型中查找有没有source属性,有,直接访问属性赋值 source = value
3. 去模型中查找有没有_source属性,有,直接访问属性赋值 _source = value
4. 找不到,直接报错 setValue:forUndefinedKey:报找不到的错误
*/
return item;
}
// 重写系统方法? 1.想给系统方法添加额外功能 2. 不想要系统方法实现
// 系统找不到就会调用这个方法
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
}
注意: 使用KVC的弊端, 当字典中嵌套字典时,或者多级嵌套的关系,使用KVC来解决非常繁琐.因此推荐使用runtime来实现字典转模型操作, 第三方框架MJExtension底层就是使用runtime来实现的.