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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值