利用Mantle库迅速建立数据模型

文章目录
  1. 快速开始
    1. 将Mantle导入项目
    2. 准备数据模型
    3. 生成数据模型

Mantle是iOS/OS X平台上比较流行的一款第三方库,由GitHub官方发布,目前在其项目主页上已经有了7500多个star。

这个库最大的优点就是能够用JSON数据迅速建立成数据模型,并由于其实现了相关协议,从而可以非常方便地进行归档等操作。

项目地址:Mantle

快速开始

将Mantle导入项目

  • 最方便的方法当然是CocoaPods,编辑Podfile

    1
    pod "Mantle"
  • 官方提供了一种麻烦的方法

    1. 把Mantle作为子模块添加到你的项目中
    2. 到Mantle的目录里运行script/bootstrap
    3. 把Mantle.xcodeproj拖到你的项目中
    4. 在Target的General里,把Mantle.framework添加到Embedded Binaries
  • 那个新生代的库管理工具Carthage同样支持引入Mantle,具体不表

准备数据模型

本文一开始就说了,Mantle的最大有点就是能迅速地将JSON转化为数据模型,从而使数据方便归档、处理。

那我们先找一个JSON数据来。我们去APIStore扒一个JSON数据过来做例子。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"errNum": 0,
"errMsg": "success",
"retData": {
"city": "北京",
"pinyin": "beijing",
"citycode": "101010100",
"date": "15-08-15",
"time": "11:00",
"postCode": "100000",
"longitude": 116.391,
"latitude": 39.904,
"altitude": "33",
"weather": "晴",
"temp": "35",
"l_tmp": "22",
"h_tmp": "35",
"WD": "无持续风向",
"WS": "微风(<10m/h)",
"sunrise": "05:25",
"sunset": "19:12"
}
}

这是一个天气预报信息的JSON数据,拿到这个数据,我们挑出有用的数据,比如错误代码,错误信息,城市名,日期,时间,最高温度,最低温度,天气情况,风向,风力。

我们新建一个类,名字叫SKWeatherCondition,是MTLModel的子类。

在头文件中引入Mantle,并声明实现MTLJSONSerializing协议。

1
2
#import <Mantle/Mantle.h>
@interface SKWeatherCondition : MTLModel<MTLJSONSerializing>

在头文件中,用@property声明我们所有需要处理的数据成员

1
2
3
4
5
6
7
8
9
10
@property (nonatomic, copy) NSString *errorMessage;
@property (nonatomic, strong) NSNumber *errorNum;
@property (nonatomic, copy) NSString *cityName;
@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) NSDate *time;
@property (nonatomic, strong) NSNumber *lowTemperature;
@property (nonatomic, strong) NSNumber *highTemperature;
@property (nonatomic, copy) NSString *condition;
@property (nonatomic, copy) NSString *windDirection;
@property (nonatomic, copy) NSString *windSpeed;

由于我们在头文件中声明了使用MTLJSONSerializing协议,所以我们需要实现JSONKeyPathsByPropertyKey方法,并将KeyPaths写好。这个方法是类方法,返回一个字典,字典的内容是@”PropertyKey” : @”KeyPath”,键值一一对应。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
+ (NSDictionary *)JSONKeyPathsByPropertyKey {
return @{
@"errorMessage" : @"errMsg",
@"errorNum" : @"errNum",
@"cityName" : @"retData.city",
@"date" : @"retData.date",
@"time" : @"retData.time",
@"lowTemperature" : @"retData.l_tmp",
@"highTemperature" : @"retData.h_tmp",
@"condition" : @"retData.weather",
@"windDirection" : @"retData.WD",
@"windSpeed" : @"retData.WS"
};
}

至此,数据模型已经准备完成。

生成数据模型

我们用NSDictionary存储得到的JSON数据:

1
*forecastDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];

创建一个SKWeatherCondition的实例对象,并让它从刚刚的字典中拿数据:

1
SKWeatherCondition *weather = [MTLJSONAdapter modelOfClass:SKWeatherCondition.class fromJSONDictionary:dictionary error:nil];

我们运行程序,如果程序没有问题,那说明我们已经完成了对JSON数据实例对象数据模型的建立。我们可以输出实例对象的成员变量,来确保数据的正常。

1
2
3
4
5
6
7
8
- (void)logWeatherCondition {
NSLog(@"错误代码:%@ 错误信息:%@", self.errorNum, self.errorMessage);
NSLog(@"城市:%@", self.cityName);
NSLog(@"发布时间:%@ %@", self.date, self.time);
NSLog(@"最低气温:%@ 最高气温:%@", self.lowTemperature, self.highTemperature);
NSLog(@"气象情况:%@", self.condition);
NSLog(@"风向:%@ 风力:%@", self.windDirection, self.windSpeed);
}

输出结果:

1
2
3
4
5
6
2015-08-15 22:40:38.964 SKMantleExample[13467:1891463] 错误代码:0 错误信息:success
2015-08-15 22:40:38.965 SKMantleExample[13467:1891463] 城市:北京
2015-08-15 22:40:38.965 SKMantleExample[13467:1891463] 发布时间:15-08-15 11:00
2015-08-15 22:40:38.965 SKMantleExample[13467:1891463] 最低气温:22 最高气温:35
2015-08-15 22:40:38.965 SKMantleExample[13467:1891463] 气象情况:晴
2015-08-15 22:40:38.965 SKMantleExample[13467:1891463] 风向:无持续风向 风力:微风(<10m/h)

一切正常。数据模型完成建立

快速开始的演示代码SKMantleExample