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;
}