相比 MJExtension,Mantle 的使用还是有些烦琐,并且效率也并不占优势,但仍有很多可取之处。
最近抽空看了一下简单记录。
- 基础数据
NSDictionary *dic = @{
@"name": @"Bob",
@"cars":@[@{@"make":@"ford",
@"year":@"1972" },
@{@"make":@"mazda",
@"year":@"2000" }],
@"owner":@{ @"name":@"arthur",
@"gender":@"male",
@"age":@101,
@"date":@"2018-09-02"
},
@"url":@"http://www.baidu.com",
@"date":@"2015-09-02",
};
- 字典转模型
//MTLJSONAdapter 适配器负责转换
TestModel *model = [MTLJSONAdapter modelOfClass:[TestModel class]
fromJSONDictionary:dic
error:nil];
NSLog(@"%@",model.name);
NSLog(@"%@",model.date);
NSLog(@"%@",model.url);
NSLog(@"%@",model.ownerName);
NSLog(@"%@",model.owner.name);
NSLog(@"%@",model.owner.gender);
NSLog(@"%@",model.owner.age);
NSLog(@"%@",model.owner.date);
CarModel *car1 = model.cars[0];
CarModel *car2 = model.cars[1];
NSLog(@"%@",car1.make);
NSLog(@"%@",car2.year);
NSLog(@"%@",model.maker);
- 模型创建
//model 必须继承自 MTLModel
//并且遵守 MTLJSONSerializing 协议
@interface TestModel : MTLModel<MTLJSONSerializing>
@property (nonatomic,retain) NSString * name;
@property (nonatomic,retain) NSDate * date;
@property (nonatomic,retain) NSURL * url;
@property (nonatomic, copy ) NSString * ownerName;
@property (nonatomic,retain) OwnerModel * owner;
@property (nonatomic,retain) NSArray * cars;
@property (nonatomic, copy) NSString * maker;
@end
- 方法实现
+(NSDictionary*)JSONKeyPathsByPropertyKey{
return
// key为 Model 属性,value 为对应 Json 的值
// 即使字段名一到处也不可省略,不可省略,不可省略。
@{
@"name":@"name",
@"date":@"date",
@"url":@"url",
@"ownerName":@"owner.name",
@"owner":@"owner",
@"cars":@"cars",
@"maker":@"cars"
};
}
//类型不一致则需要转变值的类型
+(NSValueTransformer *)JSONTransformerForKey:(NSString *)key{
//NSString-->NSDate
if ([key isEqualToString:@"date"]) {
return
[MTLValueTransformer transformerUsingForwardBlock:^id(NSString *string, BOOL *success, NSError *__autoreleasing *error) {
//NSString-->NSDate
return [self.dateFormatter dateFromString:string];
}];
//NSString --> NSURL
}else if([key isEqualToString:@"url"]){
return [MTLValueTransformer transformerUsingForwardBlock:^id(NSString * urlStr , BOOL *success, NSError *__autoreleasing *error) {
NSURL *url = [NSURL URLWithString:urlStr];
return url;
}];
//指向字典
}else if([key isEqualToString:@"owner"]){
return [MTLJSONAdapter dictionaryTransformerWithModelClass:[OwnerModel class]];
//或者
return [MTLValueTransformer transformerUsingForwardBlock:^id(NSDictionary* dic, BOOL *success, NSError *__autoreleasing *error) {
return [MTLJSONAdapter modelOfClass:[OwnerModel class] fromJSONDictionary:dic error:nil];
}];
//指向数组
}else if([key isEqualToString:@"cars"]){
return [MTLJSONAdapter arrayTransformerWithModelClass:[CarModel class]];
//指向数组中的某个值
}else if([key isEqualToString:@"maker"]){
return [MTLValueTransformer transformerUsingForwardBlock:^id(NSArray * cars , BOOL *success, NSError *__autoreleasing *error) {
return [cars.firstObject
valueForKey:@"make"];
}];
}else{
return nil;
}
}
+ (NSDateFormatter *)dateFormatter {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd";
return dateFormatter;
}
@end
网友评论