Mantle简单认识及使用

Mantle是什么?

Mantle是一个建模框架,实现了多个不同的NSCodingNSCopying方法,还添加了许多非常便利的方法允许你实现更多有用的功能,比如返回一个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;
}

你可能感兴趣的:(ios,json,Model,mantle)