阅读目录
MJExtension是JSON和模型之间最快捷方便的转换iOS框架。
能做什么?
- MJExtension是一套字典和模型之间互相转换的超轻量级框架
- MJExtension能完成的功能
- 字典(JSON)-->模型(Model)
- 模型(Model)-->字典(JSON)
- 字典数组(JSON Array)-->模型数组(Model Array)
- 模型数组(Model Array)-->字典数组(JSON Array)
- 详尽用法主要参考 main.m中的各个函数 以及NSObject+MJKeyValue.h
MJExtension和JSONModel、Mantle等框架的区别
- 转换速率:
- 最近一次测试表明:MJExtension>JSONModel>Mantle
- 各位开发者也可以自行测试
- 具体用法:
- JSONModel:要求所有模型类必须继承自JSONModel基类
- Mantle:要求所有模型类必须继承自MTModel基类
- MJExtension:不需要你的模型类继承任何特殊基类,毫无污染,毫无侵入性
如何使用MJExtension
- cocoapods导入:pod 'MJExtension'
- 手动导入:
- 将MJExtensionExample/MJExtensionExample/MJExtension文件夹中的所有源代码拽入项目中
- 导入主头文件:#import "MJExtension.h"
12345678
MJExtension.h
MJConst.h MJConst.m
MJFoundation.h MJFoundation.m
MJIvar.h MJIvar.m
MJType.h MJType.m
NSObject+MJCoding.h NSObject+MJCoding.m
NSObject+MJIvar.h NSObject+MJIvar.m
NSObject+MJKeyValue.h NSObject+MJKeyValue.m
最简单的字典转模型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
typedef enum { SexMale, SexFemale } Sex; @interface User : NSObject @property (copy, nonatomic) NSString *name; @property (copy, nonatomic) NSString *icon; @property (assign, nonatomic) int age; @property (assign, nonatomic) double height; @property (strong, nonatomic) NSNumber *money; @property (assign, nonatomic) Sex sex; @end NSDictionary *dict = @{ @ "name" : @ "Jack" , @ "icon" : @ "lufy.png" , @ "age" : @20, @ "height" : @ "1.55" , @ "money" : @100.9, @ "sex" : @(SexFemale) }; // 将字典转为User模型 User *user = [User objectWithKeyValues:dict]; NSLog(@ "name=%@, icon=%@, age=%d, height=%@, money=%@, sex=%d" , user.name, user.icon, user.age, user.height, user.money, user.sex); // name=Jack, icon=lufy.png, age=20, height=1.550000, money=100.9, sex=1 |
核心代码
- [User objectWithKeyValues:dict]