Mantle是什么?
Mantle是一个建模框架,实现了多个不同的NSCoding和NSCopying方法,还添加了许多非常便利的方法允许你实现更多有用的功能,比如返回一个json字典,以代表你的对象。很多情况下,Mantle可以作为Core Data的替代选择。
Mantle如何使用?
- 从github下载加入到项目或者通过
cocoapod导入(pod 'Mantle') - 简单自定义对象继承
MTLModel并声明<MTLJSONSerializing>协议,实现+ (NSDictionary *)JSONKeyPathsByPropertyKey协议方法。
例如对下面的jsonDic数据进行转换: "id": 12, "code": 200, "msg": "ok"
简单对象定义:
@interface SampleEntity : MTLModel<MTLJSONSerializing> @property (nonatomic, assgin) NSInteger *sampleId; //与系统id会冲突 @property (nonatomic, assgin) NSInteger code; @property (nonatomic, strong) NSString *msg; @end @implementation ESFHouseEntity + (NSDictionary *)JSONKeyPathsByPropertyKey { NSMutableDictionary *mapping = [[NSDictionary mtl_identityPropertyMapWithModel:self] mutableCopy]; mapping[@"sampleId"] = @"id"; //解决与系统id会冲突的问题 return mapping; } @end
讯享网
jsonDic数据转SampleEntity:
讯享网SampleEntity *entity = [MTLJSONAdapter modelOfClass:[SampleEntity class] fromJSONDictionary:jsonDic error:nil];
- 对jsonArr数组进行解析:
讯享网
[ { "age": 0, "id": 1, "name": "name0" }, { "age": 1, "id": 2, "name": "name1" }, { "age": 2, "id": 3, "name": "name2" } ]
对象定义:
@interface SampleArrEntity : MTLModel<MTLJSONSerializing> @property (nonatomic, assgin) NSInteger *sampleArrId; //与系统id会冲突 @property (nonatomic, assgin) NSInteger age; @property (nonatomic, strong) NSString *name; @end @implementation ESFHouseEntity + (NSDictionary *)JSONKeyPathsByPropertyKey { NSMutableDictionary *mapping = [[NSDictionary mtl_identityPropertyMapWithModel:self] mutableCopy]; mapping[@"sampleArrId"] = @"id"; //解决与系统id会冲突的问题 return mapping; } @end
jsonArr数据转SampleArrEntity:
讯享网NSArray *SampleArrEntityArr = [MTLJSONAdapter modelsOfClass:[SampleArrEntity class] fromJSONArray:jsonArr error:nil];
- 对于比较复杂的json数据:
{ "houseLookByAgentMsgList": [ { "agent": { "credit": 0, "id": }, "lookedDate": "2015-07-29" }, { "agent": { "credit": 0, "id": }, "lookedDate": "2015-07-29" } ], "similarHouseMsgList": [ { "id": , "picture": "http://fs.xxx.net/image/000/003/919/ValtlhStZdQdZ93OaQ-gvNgdI_I.jpg" }, { "id": , "picture": "http://fs.xxx.net/image/000/003/919/ValtlhStZdQdZ93OaQ-gvNgdI_I.jpg" } ], "houseId": 123 }
对象定义:
讯享网@class ESFAgentEntity,ESFHouseListEntity,HouseLookByAgentMsg; @interface ComplexEntity : MTLModel<MTLJSONSerializing> @property (nonatomic, strong) NSArray *houseLookByAgentMsgList; //经纪人带看记录列表<HouseLookByAgentMsg> @property (nonatomic, strong) NSArray *similarHouseMsgList;//相似房源<ESFHouseListEntity> @property (nonatomic, strong) NSString *houseId; @end #pragma mark - 经纪人带看记录对象 @interface HouseLookByAgentMsg : MTLModel <MTLJSONSerializing> @property (nonatomic, strong) ESFAgentEntity *agent;//经纪人实体 @property (nonatomic, strong) NSString *lookedDate;//"2014-12-05", // 带看时间 @end #pragma mark - 经纪人对象 @interface ESFAgentEntity : MTLModel <MTLJSONSerializing> @property (nonatomic, assign) NSInteger credit; @property (nonatomic, strong) NSInteger agentId;//与系统id会冲突 @end #pragma mark - 相似房源对象 @interface ESFHouseListEntity : MTLModel <MTLJSONSerializing> @property (nonatomic, assign) NSString *picture; @property (nonatomic, strong) NSInteger houseListId;//与系统id会冲突 @end @implementation ComplexEntity + (NSDictionary *)JSONKeyPathsByPropertyKey { NSMutableDictionary *mapping = [[NSDictionary mtl_identityPropertyMapWithModel:self] mutableCopy]; return mapping; } //经纪人带看记录列表<HouseLookByAgentMsg>进行解析 + (NSValueTransformer *)houseLookByAgentMsgListJSONTransformer { return [MTLJSONAdapter arrayTransformerWithModelClass:HouseLookByAgentMsg.class]; } //相似房源<ESFHouseListEntity>进行解析 + (NSValueTransformer *)similarHouseMsgListJSONTransformer { return [MTLJSONAdapter arrayTransformerWithModelClass:ESFHouseListEntity.class]; } @end @implementation HouseLookByAgentMsg + (NSDictionary *)JSONKeyPathsByPropertyKey { NSMutableDictionary *mapping = [[NSDictionary mtl_identityPropertyMapWithModel:self] mutableCopy]; return mapping; } //对经纪人实体agent进行解析 + (NSValueTransformer *)agentJSONTransformer { return [MTLJSONAdapter dictionaryTransformerWithModelClass:ESFAgentEntity.class]; } @end @implementation ESFHouseListEntity + (NSDictionary *)JSONKeyPathsByPropertyKey { NSMutableDictionary *mapping = [[NSDictionary mtl_identityPropertyMapWithModel:self] mutableCopy]; mapping[@"houseListId"] = @"id";//与系统id会冲突 return mapping; } @end @implementation ESFAgentEntity + (NSDictionary *)JSONKeyPathsByPropertyKey { NSMutableDictionary *mapping = [[NSDictionary mtl_identityPropertyMapWithModel:self] mutableCopy]; mapping[@"agentId"] = @"id"; return mapping; }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/79873.html