0%

RunTime - 字典转模型

NSObject+ModelExtension.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#import <Foundation/Foundation.h>

@protocol NSObjectDelegate <NSObject>

@optional
+ (NSDictionary *)arrayContainModelClass; ///< array 转字典
@optional
+ (NSDictionary *)modelContainModelClass; /// < array 转model

@end


@interface NSObject (ModelExtension)

+ (instancetype)modelWithDict:(id)dict;

@end

NSObject+ModelExtension.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#import "NSObject+ModelExtension.h"
#import <objc/runtime.h>

@implementation NSObject (ModelExtension)

+ (instancetype)modelWithDict:(id)dict {
id obj = [[self alloc] init];

unsigned int count;
Ivar *ivarList = class_copyIvarList(self, &count);
for (int i = 0; i < count; i ++) {
Ivar ivar = ivarList[i];

// 获取成员属性
NSString *name = [NSString stringWithUTF8String:ivar_getName(ivar)];

// 处理成员属性名 -> 字典中的key 去掉下划线
NSString *key = [name substringFromIndex:1];

// 根据成员属性名去字典查找对应的value
id value = dict[key];

// 二级转换: 如果字典中还有字典,也需要把对应的字典转换成模型
if ([value isKindOfClass:[NSDictionary class]]) {

// 获取成员变量的类型
NSString *type = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];

// 裁剪类型字符串
NSRange range = [type rangeOfString:@"\""];

type = [type substringFromIndex:range.location + range.length];

range = [type rangeOfString:@"\""];

type = [type substringFromIndex:range.location];

// 根据字符串类型生成类对象
Class moduleClass = NSClassFromString(type);

if (moduleClass) {
value = [moduleClass modelWithDict:value];
}
}

// 三级转换: NSArray 中也是字典 把数组中的字典转化成模型
if ([value isKindOfClass:[NSArray class]]) {
// 判断对应类有没有实现字典数组转模型数组的协议
if ([self respondsToSelector:@selector(arrayContainModelClass)]) {
// 转换成id类型 就能调用任何对象的方法
id idSelf = self;

// 获取数组中字典对应的模型
NSString *type = [idSelf arrayContainModelClass][key];
//生成模型
Class classModel = NSClassFromString(type);
NSMutableArray *arrayM = [NSMutableArray array];
// 遍历字典数组 生成模型数组
for (NSDictionary *dict in value) {
// 字典模型
id model = [classModel modelWithDict:dict];
[arrayM addObject:model];
}
// 把模型数组赋值给value
value = arrayM;
}

// 模型里面套模型
if ([self respondsToSelector:@selector(modelContainModelClass)]) {
id idSelf = self;
NSString *type = [idSelf modelContainModelClass][key];
Class classModel = NSClassFromString(type);
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in value) {
id model = [classModel modelWithDict:dict];
[arrayM addObject:model];
}
value = arrayM;
}
}
if (value) {// 有值,才需要给模型的属性赋值
// 利用KVC给模型的属性赋值
[obj setValue:value forKey:key];
}
}
return obj;
}

@end

使用 以请求豆瓣电影250为例

DouBanModel.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#import <Foundation/Foundation.h>
#import "NSObject+ModelExtension.h"

@interface MovieInfo : NSObject <NSCoding>

@property (nonatomic, strong) NSDictionary *rating;
@property (nonatomic, assign) NSInteger id;
@property (nonatomic, copy) NSString *original_title;
@property (nonatomic, assign) NSInteger collect_count;
@property (nonatomic, copy) NSArray *directors;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *year;
@property (nonatomic, copy) NSArray *casts;
@property (nonatomic, copy) NSArray *genres;
@property (nonatomic, strong) NSDictionary *images;
@property (nonatomic, copy) NSString *subtype;
@property (nonatomic, copy) NSString *alt;

@end


@interface DouBanModel : NSObject <NSObjectDelegate, NSCoding>

@property (nonatomic, assign) NSInteger count;
@property (nonatomic, assign) NSInteger start;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) NSArray<MovieInfo *> *subjects;

@end

DouBanModel.m

1
2
3
4
5
@implementation DouBanModel
+ (NSDictionary *)modelContainModelClass {
return @{@"subjects": NSStringFromClass([MovieInfo class])};
}
@end

ViewController.m 网络请求

1
2
3
4
5
6
7
8
9
10
 [[NetWorkTooth shendeInstence] requestWithUrl:@"https://api.douban.com/v2/movie/top250?start=0&count=50" success:^(NSDictionary *dictData) {
DouBanModel *model = [DouBanModel modelWithDict:dictData];
[self saveData:model];
NSLog(@"请求的到的:");
for (MovieInfo *info in model.subjects) {
NSLog(@"%@ - %@", info.title, info.year);
}
} failed:^(NSError *error) {

}];

Demo 地址

https://github.com/GhostClock/Runtime-Demo