一行代码实现 字典 - 模型 转换。
pod 'WKModel' 并 #import <NSObject+WKModel.h>
构造字典:
NSDictionary *detail1 = @{@"food":@"Rice", @"drink":@"Juice"};
NSDictionary *detail2 = @{@"food":@"Noodles", @"drink":@"Tea"};
NSArray *arr = @[detail1, detail2];
NSDictionary *dict = @{ @"name":@"Welkin",
@"age":@22,
@"phone":@"110",
@"detail":detail1,
@"list":arr};
一行代码转模型:
Model *model = [Model wk_modelFromDictionary:dict];
结果:
模型属性列表:
@interface Model : NSObject
@property (copy, nonatomic) NSString *ID;
@property (copy, nonatomic) NSString *food;
@property (copy, nonatomic) NSString *drink;
@property (copy, nonatomic) NSString *property;
一行代码转字典:
NSDictionary *dict = [model wk_makeDictionary];
结果:
需要在模型中实现如下方法,指明其中包含的模型类型:
- (NSDictionary *)wk_objectProperties {
return @{@"detail":[Detail class]};
}
需要在模型中实现如下方法,指明数组中模型的类型:
- (NSDictionary *)wk_arrayProperties {
return @{@"list":[Detail class]};
}
需要在模型中实现如下方法,指明 key 对应的属性名:
- (NSDictionary *)wk_renamedProperties {
return @{@"id":@"ID", @"pro":@"property"};
}
WKModel 采用 MIT许可证 发布。