Mantle简单认识及使用
来源:互联网 发布:mac怎样关闭后台程序 编辑:程序博客网 时间:2024/06/06 07:52
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": 206591 }, "lookedDate": "2015-07-29" }, { "agent": { "credit": 0, "id": 224354 }, "lookedDate": "2015-07-29" } ], "similarHouseMsgList": [ { "id": 517109, "picture": "http://fs.xxx.net/image/000/003/919/ValtlhStZdQdZ93OaQ-gvNgdI_I.jpg" }, { "id": 517109, "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;}
1 0
- Mantle简单认识及使用
- Mantle 简单使用
- ListView,ListActivity的认识及简单使用
- Mantle 初步使用
- Mantle 初步使用
- Mantle 初步使用
- Mantle 初步使用
- ios Mantle使用小记
- Mantle 初步使用
- Mantle的使用
- Mantle 快速使用上手
- ios Mantle使用小记
- Mantle使用(摘自GitHub)
- Mantle 初步使用
- Mantle 初步使用
- Mantle
- Mantle
- Mantle
- HBase 1.1.1 发布(分布式数据库)
- ArcGIS教程:分水岭的工作原理
- android调试log输出及类似C/C++中的__FILE__、__FUNC__、__LINE__等功能
- 求二叉树的先序遍历
- forward和redirect的区别
- Mantle简单认识及使用
- oracle密码过期ORA-28002: 7天之后口令将过期的解决方法
- Django+ajax实现页面底部加载
- Python中super的用法
- iOS开发-Day17-OC NSString / NSMutableString常用方法
- 关于Blocking IO, Non-Blocking IO 和 Asynchronous I/O的理解
- html中列表的嵌套
- Java文件路径(getResource)
- C/C++在线编译器