记录寄己走过的路

MJExtension框架学习

MJExtension字典转模型框架

1
2
3
4
A fast, convenient and nonintrusive conversion between JSON
and model.
转换速度快、使用简单方便的字典转模型框架

 GitHub

Contents


作用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1.MJExtension是一套字典和模型之间互相转换的超轻量级框架
2.JSON --> Model、Core Data Model
3.JSONString --> Model、Core Data Model
4.Model、Core Data Model --> JSON
5.JSON Array --> Model Array、Core Data Model Array
6.JSONString --> Model Array、Core Data Model Array
7.Model Array、Core Data Model Array --> JSON Array
8.Coding all properties of model in one line code.
只需要一行代码,就能实现模型的所有属性进行Coding(归档和解档)
1.简单的字典 --> 模型
2.JSON字符串 --> 模型
3.复杂的字典 --> 模型 (模型里面包含了模型)
4.复杂的字典 --> 模型 (模型的数组属性里面又装着模型)
5.复杂的字典 --> 模型(模型属性名和字典的key不一样)
6.字典数组 --> 模型数组
7.模型 --> 字典
8.模型数组 --> 字典数组
9.字典 --> CoreData模型
10.归档与解档NSCoding
11.过滤字典的值
安装
1
2
3
4
5
使用CocoaPods `pod 'MJExtension'`
手动导入
1.将`MJExtension`文件夹中的所有源代码拽入项目中
2.导入主头文件:`#import "MJExtension.h"`

Getting Started【开始使用】


1.JSON -> Model【最简单的字典转模型】

核心方法:mj_objectWithKeyValues:

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
模型类User定义
typedef enum {    
SexMale,
SexFemale
} Sex;
@interface User : NSObject
@property (copy, nonatomic) NSString *name;/* 姓名 */
@property (copy, nonatomic) NSString *icon;/* 头像 */
@property (assign, nonatomic) unsigned int age;/* 年龄 */
@property (copy, nonatomic) NSString *height;/* 身高 */
@property (strong, nonatomic) NSNumber *money;/* 资产 */
@property (assign, nonatomic) Sex sex;/* 性别 */@property (assign, nonatomic, getter=isGay) BOOL gay;/* 是否是同性恋 */
@end
// 简单的字典
NSDictionary *dict_user = @{                        
@"name" : @"Jack",                        
@"icon" : @"lufy.png",                        
@"age" : @20,                        
@"height" : @"1.55",                        
@"money" : @100.9,                        
@"sex" :
@(SexFemale),/* 枚举需要使用NSNumber包装 */                        
@"gay" : @YES                        
};
User *user = [User mj_objectWithKeyValues:dict_user];
NSLog(@"MJ-%@-%@-%u-%@-%@-%u-%d",user.name,user.icon,user.age,user.height,user.money,user.sex,user.gay);
//打印结果
//2016-07-04 11:06:59.746 PPDemos[2432:73824] MJ-Jack-lufy.png-20-1.55-100.9-1-1

2.JSONString -> Model【JSON字符串转模型】

核心方法:mj_objectWithKeyValues:

1
2
3
4
5
6
// 定义一个JSON字符串
NSString *jsonStr = @"{\"name\":\"Jack\", \"icon\":\"lufy.png\", \"age\":20}";
User *user = [User mj_objectWithKeyValues:jsonStr];
NSLog(@"MJ---%@----%@---%u",user.name,user.icon,user.age);
// 打印结果
// 2016-07-04 11:16:04.655 PPDemos[2563:78561] MJ---Jack----lufy.png---20

3.复杂的字典【模型中嵌套模型】

核心方法:mj_objectWithKeyValues:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//复杂的字典[模型中有个数组属性,数组里面又要装着其他模型的字典]
NSDictionary *dict_m8m = @{              
@"text" : @"Agree!Nice weather!",              
@"user" : @{                      
@"name" : @"Jack",                      
@"icon" : @"lufy.png"                      
},              
@"retweetedStatus" : @{                      
@"text" : @"Nice weather!",                      
@"user" : @{                              
@"name" : @"Rose",                              
@"icon" : @"nami.png"                          
                                         }                      
}              
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#import <Foundation/Foundation.h>
@class User;
@class Status;
//Status模型
@interface Status : NSObject
@property (copy, nonatomic) NSString *text;
@property (strong, nonatomic) User *user;/* 其他模型类型 */
@property (strong, nonatomic) Status *retweetedStatus;/* 自我模型类型 */
@end
//
// 字典转模型,模型里面含有模型
Status *status = [Status mj_objectWithKeyValues:dict_m8m];
NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon; NSLog(@"mj-----text=%@, name=%@, icon=%@", text, name, icon);
NSString *text2 = status.retweetedStatus.text;
NSString *name2 = status.retweetedStatus.user.name;
NSString *icon2 = status.retweetedStatus.user.icon;
NSLog(@"mj-----text2=%@, name2=%@, icon2=%@", text2, name2, icon2);
// 打印结果
//2016-07-04 11:45:39.675 PPDemos[2781:87089] mj-----text=Agree!Nice weather!, name=Jack, icon=lufy.png
//2016-07-04 11:45:39.675 PPDemos[2781:87089] mj-----text2=Nice weather!, name2=Rose, icon2=nami.png
4.【模型中有个数组属性,数组里面又要装着其他模型】

核心方法:mj_objectWithKeyValues:mj_objectClassInArray

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@interface Ad : NSObject
@property (copy, nonatomic) NSString *image;
@property (copy, nonatomic) NSString *url;
@end
@interface StatusResult : NSObject/** 数组中存储模型Status类型数据 */
@property (strong, nonatomic) NSMutableArray *statuses;/** 数组中存储模型Ad类型数据 */
@property (strong, nonatomic) NSArray *ads;
@property (strong, nonatomic) NSNumber *totalNumber;
@end   
#import "StatusResult.h"
#import "MJExtension.h"
@implementation StatusResult/* 数组中存储模型数据,需要说明数组中存储的模型数据类型,实现该方法 */
+(NSDictionary *)mj_objectClassInArray{    
return @{            
@"statuses" : @"Status",            
@"ads" : @"Ad"            
};
}
@end

在VC里实现以下来解析数据

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//模型中有个数组属性,数组里面又要装着其他模型
NSDictionry *dict_m8a = @{
@"statuses" : @[
@{
@"text" : @"Nice weather!",
@"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
},
@{
@"text" : @"Go camping tomorrow!",
@"user" : @{
@"name" : @"Jack",
@"icon" : @"lufy.png"
}
}
],
@"ads" : @[
@{
@"image" : @"ad01.png",
@"url" : @"http://www.ad01.com"
},
@{
@"image" : @"ad02.png",
@"url" : @"http://www.ad02.com"
}
],
@"totalNumber" : @"2014"
};
【重点,核心】>>数组中存储模型数据,需要说明数组中存储的模型数据类型<<
/*
[StatusResult mj_setupObjectClassInArray:^NSDictionary *{
return @{
@"statuses" : @"Status",
// @"statuses" : [Status class],
@"ads" : @"Ad"
// @"ads" : [Ad class]
};
}];
// Equals: StatusResult.m implements +mj_objectClassInArray method.
*/
// 以上方法在VC里写,如果多个地方解析该model,就要写多次,最好在model的.m文件写!
// 字典转模型,支持模型的数组属性里面又装着模型
StatusResult *result = [StatusResult mj_objectWithKeyValues:dict_m8a];
// 打印博主信息
for (Status *status in result.statuses) {
NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon;
NSLog(@"mj---text=%@, name=%@, icon=%@", text, name, icon);
}
// 打印广告
for (Ad *ad in result.ads) {
NSLog(@"mj---image=%@, url=%@", ad.image, ad.url);
}
// 打印结果
//2016-07-04 13:47:58.994 PPDemos[3353:113055] mj---text=Nice weather!, name=Rose, icon=nami.png
//2016-07-04 13:47:58.995 PPDemos[3353:113055] mj---text=Go camping tomorrow!, name=Jack, icon=lufy.png
//2016-07-04 13:47:58.995 PPDemos[3353:113055] mj---image=ad01.png, url=http://www.ad01.com
//2016-07-04 13:47:58.995 PPDemos[3353:113055] mj---image=ad02.png, url=http://www.ad02.com

5.【模型中的属性名和字典中的key不相同(或者需要多级映射)】

核心方法:mj_objectWithKeyValues:mj_replacedKeyFromPropertyName

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
30
@interface Bag : NSObject
@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) double price;
@end
#import <Foundation/Foundation.h>
@class Bag;
@interface Student : NSObject
@property (copy, nonatomic) NSString *ID;
@property (copy, nonatomic) NSString *desc;
@property (copy, nonatomic) NSString *nowName;
@property (copy, nonatomic) NSString *oldName;
@property (copy, nonatomic) NSString *nameChangedTime;
@property (strong, nonatomic) Bag *bag;
@end
// 多级映射,用点语法设置
@implementation Student
/* 设置模型属性名和字典key之间的映射关系 */
+(NSDictionary *)mj_replacedKeyFromPropertyName
{
/* 返回的字典,key为模型属性名,value为转化的字典的多级key */
return @{
@"ID" : @"id",
@"desc" : @"desciption",
@"oldName" : @"name.oldName",
@"nowName" : @"name.newName",
@"nameChangedTime" : @"name.info[1].nameChangedTime",
@"bag" : @"other.bag"
};
}
@end

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
30
31
32
33
34
35
36
37
38
39
40
41
NSDictionry *dict_nokey = @{
@"id" : @"20",
@"desciption" : @"kids",
@"name" : @{
@"newName" : @"lufy",
@"oldName" : @"kitty",
@"info" : @[
@"test-data",
@{
@"nameChangedTime" : @"2013-08"
}
]
},
@"other" : @{
@"bag" : @{
@"name" : @"a red bag",
@"price" : @100.7
}
}
};
//
// // How to map
// [Student mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
// return @{
// @"ID" : @"id",
// @"desc" : @"desciption",
// @"oldName" : @"name.oldName",
// @"nowName" : @"name.newName",
// @"nameChangedTime" : @"name.info[1].nameChangedTime",
// @"bag" : @"other.bag"
// };
// }];
// // Equals: Student.m implements +mj_replacedKeyFromPropertyName method.
// 字典转模型,支持多级映射
Student *stu = [Student mj_objectWithKeyValues:dict_nokey];
// 打印
NSLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@",
stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime);
NSLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price);
//2016-07-04 14:20:28.082 PPDemos[3602:126004] ID=20, desc=kids, oldName=kitty, nowName=lufy, nameChangedTime=2013-08
//2016-07-04 14:20:28.082 PPDemos[3602:126004] bagName=a red bag, bagPrice=100.700000
6.【将一个字典数组转成模型数组】

核心方法:mj_objectArrayWithKeyValuesArray:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
NSArray *dictArray = @[
@{
@"name" : @"Jack",
@"icon" : @"lufy.png"
},
@{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
];
// 字典数组转模型数组,使用的是mj_objectArrayWithKeyValuesArray:方法
NSArray *userArray = [User mj_objectArrayWithKeyValuesArray:dictArray];
//打印
for (User *user in userArray) {
NSLog(@"name=%@, icon=%@", user.name, user.icon);
}
// name=Jack, icon=lufy.png
// name=Rose, icon=nami.png

7.【将一个模型转成字典】

核心方法:mj_keyValues:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//创建一个模型对象
User *user = [[User alloc] init];
user.name = @"Jack";
user.icon = @"lufy.png";
Status *status = [[Status alloc] init];
status.user = user;
status.text = @"Nice mood!";
//模型转字典,使用的是mj_keyValues属性
NSDictionary *statusDict = status.mj_keyValues;
NSLog(@"%@", statusDict);
/*
{
text = "Nice mood!";
user = {
icon = "lufy.png";
name = Jack;
};
}
*/

8.【将一个模型数组转成字典数组】

核心代码:mj_keyValuesArrayWithObjectArray

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 创建模型数组
User *user1 = [[User alloc] init];
user1.name = @"Jack";
user1.icon = @"lufy.png";
User *user2 = [[User alloc] init];
user2.name = @"Rose";
user2.icon = @"nami.png";
NSArray *userArray = @[user1, user2];
//模型数组转字典数组,使用的是mj_keyValuesArrayWithObjectArray:方法
NSArray *dictArray = [User mj_keyValuesArrayWithObjectArray:userArray];
NSLog(@"%@", dictArray);
/*
(
{
icon = "lufy.png";
name = Jack;
},
{
icon = "nami.png";
name = Rose;
}
)
*/

9.Core Data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
NSDictionary *dict = @{
@"name" : @"Jack",
@"icon" : @"lufy.png",
@"age" : @20,
@"height" : @1.55,
@"money" : @"100.9",
@"sex" : @(SexFemale),
@"gay" : @"true"
};
//字典转为CoreData模型
NSManagedObjectContext *context = nil;
User *user = [User mj_objectWithKeyValues:dict
context:context];
[context save:nil];
10.归档与解档NSCoding
1
2
3
4
5
6
7
8
9
10
11
12
13
@interface Bag : NSObject <NSCoding>
@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) double price;
@end
#import "MJExtension.h"
@implementation Bag
//添加了下面的宏定义
MJExtensionCodingImplementation
/* 实现下面的方法,说明哪些属性不需要归档和解档 */
+ (NSArray *)mj_ignoredCodingPropertyNames{
return @[@"name"];
}
@end
1
2
3
4
5
6
7
8
9
10
11
12
//创建模型
Bag *bag = [[Bag alloc] init];
bag.name = @"Red bag";
bag.price = 200.8;
//获取归档路径
NSString *file = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop/bag.data"];
//归档
[NSKeyedArchiver archiveRootObject:bag toFile:file];
//解档
Bag *decodedBag = [NSKeyedUnarchiver unarchiveObjectWithFile:file];
NSLog(@"name=%@, price=%f", decodedBag.name, decodedBag.price);
// name=(null), price=200.800000
11.【统一转换属性名(比如驼峰转下划线)】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Dog
#import "MJExtension.h"
@implementation Dog
+ (NSString *)mj_replacedKeyFromPropertyName121:(NSString *)propertyName
{
// nickName -> nick_name
return [propertyName mj_underlineFromCamel];
}
@end
// NSDictionary
NSDictionary *dict = @{
@"nick_name" : @"旺财",
@"sale_price" : @"10.5",
@"run_speed" : @"100.9"
};
// NSDictionary -> Dog
Dog *dog = [Dog mj_objectWithKeyValues:dict];
// printing
NSLog(@"nickName=%@, scalePrice=%f runSpeed=%f", dog.nickName, dog.salePrice, dog.runSpeed);
12.【过滤字典的值(比如字符串日期处理为NSDate、字符串nil处理为@””)】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@interface Book: NSObject
@property (copy, nonatomic) NSString *name;
@property (strong, nonatomic) NSDate *publishedTime;
@end
#import "MJExtension.h"
@implementation Book
/* 转化过程中对字典的值进行过滤和进一步转化 */
- (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property
{
if ([property.name isEqualToString:@"publisher"]) {
if (oldValue == nil) {
return @"";
}
} else if (property.type.typeClass == [NSDate class]) {
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd";
return [fmt dateFromString:oldValue];
}
return oldValue;
}
@end
1
2
3
4
5
6
7
8
NSDictionary *dict = @{
@"name" : @"5分钟突破iOS开发",
@"publishedTime" : @"2011-09-10"
};
//字典转模型,过滤name为nil的情况,把NSString转为NSDate
Book *book = [Book mj_objectWithKeyValues:dict];
//打印
NSLog(@"name=%@, publishedTime=%@", book.name, book.publishedTime);

1.模型中有个数组属性,数组里面又要装着其他模型

Plist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//1.把字典数组转换为模型数组
//使用MJExtension框架进行字典转模型
self.videos = [XMGVideo objectArrayWithKeyValuesArray:videoArray];
//2.重命名模型属性的名称
//第一种重命名属性名称的方法,有一定的代码侵入性
//设置字典中的id被模型中的ID替换
+(NSDictionary *)replacedKeyFromPropertyName {
    return @{
            @"ID":@"id"
            };
}
//第二种重命名属性名称的方法,代码侵入性为零
[LNVideo setupReplacedKeyFromPropertyName:^NSDictionary *{
    return @{    
            @"ID":@"id"
            };
}];
//3.MJExtension框架内部实现原理-运行时

这个框架有太多的东西值得我们去学习推敲,方方面面都透露着作者以及众多贡献者的智慧,我们使用起来也一定会受益良多。

期待


  • 如果在阅读过程中遇到 error || new ideas,希望你能 messages 我,我会及时改正谢谢。
  • 点击右上角的 喜欢 和 订阅Rss 按钮,可以收藏本仓库,并在 Demo 更新时收到邮件通知。
❄︎ 本文结束    感谢简阅 ^_^. ❄︎

本文标题:MJExtension框架学习

文章作者:寄己的路

原始链接:https://sunyonghui.github.io/SourceAnnotations/MJExtensionLibrary.html

版权声明: 署名-非商业性使用-禁止演绎 4.0 国际 本博客所有文章除特别声明外均为原创,转载务必请「注明出处链接(可点击) - 作者」,并通过E-mail等方式告知,谢谢合作!