引用于如下两篇博客
https://github.com/Mantle/Mantle
http://blog.csdn.net/afdl9999/article/details/45631525
注意配置在header search path里配置三房库”$(SRCROOT)路径”
1.新建一个类如ml继承于MTLModel
2.遵守协议MTLJSONSerializing
3.如下
@interface ml : MTLModel<MTLJSONSerializing>
@property (nonatomic,strong) NSNumber *code;
@property (nonatomic,strong) NSString *msg;
@end
5.将下载的数据与我们想要显示的数据进行对应
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"code":@"code",
@"msg" :@"msg",
};
}
6.进行相对应的转换方法
+(NSValueTransformer *)JSONTransformerForKey:(NSString *)key{
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^(NSNumber *dateNum) {
return [NSDate dateWithTimeIntervalSince1970:dateNum.floatValue];
} reverseBlock:^(NSDate *date) {
return [NSString stringWithFormat:@"%f",[date timeIntervalSince1970]];
}];
}
7.将model数据转换为json数据
+(NSDictionary *)JSONDictionaryFromModel:(MTLModel<MTLJSONSerializing> *)model{
return nil;
}
8.对象的存储
[NSKeyArchiver archiveRootObject:model toFile:path];
9.对象的解档
ml *model = [NSKeyedUnarchiver unarchiveObjectWithFile:@""];