世界上轉(zhuǎn)換速度最快、使用最簡(jiǎn)單方便的字典轉(zhuǎn)模型框架
能做什么挂据?
MJExtension是一套字典和模型之間互相轉(zhuǎn)換的超輕量級(jí)框架
MJExtension能完成的功能
字典(JSON) --> 模型(Model)运提、CoreData模型(Core Data Model)
JSON字符串 --> 模型(Model)术徊、CoreData模型(Core Data Model)
模型(Model)盖腕、CoreData模型(Core Data Model) --> 字典(JSON)
字典數(shù)組(JSON Array) --> 模型數(shù)組(Model Array)钝域、Core Data模型數(shù)組(Core Data Model Array)
JSON字符串 --> 模型數(shù)組(Model Array)妨退、Core Data模型數(shù)組(Core Data Model Array)
模型數(shù)組(Model Array)妇萄、Core Data模型數(shù)組(Core Data Model Array) --> 字典數(shù)組(JSON Array)
只需要一行代碼蜕企,就能實(shí)現(xiàn)模型的所有屬性進(jìn)行Coding(歸檔和解檔)
詳盡用法主要參考 main.m中的各個(gè)函數(shù) 以及 NSObject+MJKeyValue.h
MJExtension和JSONModel、Mantle等框架的區(qū)別
轉(zhuǎn)換速率:
最近一次測(cè)試表明:MJExtension > JSONModel > Mantle
各位開發(fā)者也可以自行測(cè)試
具體用法:
JSONModel:要求所有模型類必須繼承自JSONModel基類
Mantle:要求所有模型類必須繼承自MTModel基類
MJExtension:不需要你的模型類繼承任何特殊基類冠句,也不需要修改任何模型代碼轻掩,毫無污染,毫無侵入性
如何使用MJExtension
cocoapods導(dǎo)入:pod 'MJExtension'
手動(dòng)導(dǎo)入:
將MJExtensionExample/MJExtensionExample/MJExtension文件夾中的所有源代碼拽入項(xiàng)目中
導(dǎo)入主頭文件:#import "MJExtension.h"
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
最簡(jiǎn)單的字典轉(zhuǎn)模型
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)
};
// 將字典轉(zhuǎn)為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]
JSON字符串轉(zhuǎn)模型
// 1.定義一個(gè)JSON字符串
NSString *jsonString = @"{\"name\":\"Jack\", \"icon\":\"lufy.png\", \"age\":20}";
// 2.將JSON字符串轉(zhuǎn)為User模型
User *user = [User objectWithKeyValues:jsonString];
// 3.打印User模型的屬性
NSLog(@"name=%@, icon=%@, age=%d", user.name, user.icon, user.age);
// name=Jack, icon=lufy.png, age=20
核心代碼
[User objectWithKeyValues:dict]
模型中嵌套模型
@interface Status : NSObject
/** 微博文本內(nèi)容 */
@property (copy, nonatomic) NSString *text;
/** 微博作者 */
@property (strong, nonatomic) User *user;
/** 轉(zhuǎn)發(fā)的微博 */
@property (strong, nonatomic) Status *retweetedStatus;
@end
/***********************************************/
NSDictionary *dict = @{
@"text" : @"是啊懦底,今天天氣確實(shí)不錯(cuò)唇牧!",
@"user" : @{
@"name" : @"Jack",
@"icon" : @"lufy.png"
},
@"retweetedStatus" : @{
@"text" : @"今天天氣真不錯(cuò)!",
@"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
}
};
// 將字典轉(zhuǎn)為Status模型
Status *status = [Status objectWithKeyValues:dict];
NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon;
NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
// text=是啊聚唐,今天天氣確實(shí)不錯(cuò)丐重!, name=Jack, icon=lufy.png
NSString *text2 = status.retweetedStatus.text;
NSString *name2 = status.retweetedStatus.user.name;
NSString *icon2 = status.retweetedStatus.user.icon;
NSLog(@"text2=%@, name2=%@, icon2=%@", text2, name2, icon2);
// text2=今天天氣真不錯(cuò)!, name2=Rose, icon2=nami.png
核心代碼
[Status objectWithKeyValues:dict]
模型中有個(gè)數(shù)組屬性拱层,數(shù)組里面又要裝著其他模型
@interface Ad : NSObject
@property (copy, nonatomic) NSString *image;
@property (copy, nonatomic) NSString *url;
@end
@interface StatusResult : NSObject
/** 存放著一堆的微博數(shù)據(jù)(里面都是Status模型) */
@property (strong, nonatomic) NSMutableArray *statuses;
/** 存放著一堆的廣告數(shù)據(jù)(里面都是Ad模型) */
@property (strong, nonatomic) NSArray *ads;
@property (strong, nonatomic) NSNumber *totalNumber;
@end
/***********************************************/
// StatusResult類中的statuses數(shù)組中存放的是Status模型
// StatusResult類中的ads數(shù)組中存放的是Ad模型
[StatusResult setupObjectClassInArray:^NSDictionary *{
return @{
@"statuses" : @"Status",
@"ads" : @"Ad"
};
}];
// 相當(dāng)于在StatusResult.m中實(shí)現(xiàn)了+objectClassInArray方法
NSDictionary *dict = @{
@"statuses" : @[
@{
@"text" : @"今天天氣真不錯(cuò)弥臼!",
@"user" : @{
@"name" : @"Rose",
@"icon" : @"nami.png"
}
},
@{
@"text" : @"明天去旅游了",
@"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"
};
// 將字典轉(zhuǎn)為StatusResult模型
StatusResult *result = [StatusResult objectWithKeyValues:dict];
NSLog(@"totalNumber=%@", result.totalNumber);
// totalNumber=2014
// 打印statuses數(shù)組中的模型屬性
for (Status *status in result.statuses) {
NSString *text = status.text;
NSString *name = status.user.name;
NSString *icon = status.user.icon;
NSLog(@"text=%@, name=%@, icon=%@", text, name, icon);
}
// text=今天天氣真不錯(cuò)!, name=Rose, icon=nami.png
// text=明天去旅游了, name=Jack, icon=lufy.png
// 打印ads數(shù)組中的模型屬性
for (Ad *ad in result.ads) {
NSLog(@"image=%@, url=%@", ad.image, ad.url);
}
// image=ad01.png, url=http://www.ad01.com
// image=ad02.png, url=http://www.ad02.com
核心代碼
調(diào)用+ (void)setupObjectClassInArray:方法
[StatusResult objectWithKeyValues:dict]
提醒一句:如果NSArray\NSMutableArray屬性中存放的不希望是模型根灯,而是NSNumber径缅、NSString等基本數(shù)據(jù),那么就不需要調(diào)用+ (void)setupObjectClassInArray:方法
模型中的屬性名和字典中的key不相同(或者需要多級(jí)映射)
@interface Bag : NSObject
@property (copy, nonatomic) NSString *name;
@property (assign, nonatomic) double price;
@end
@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
/***********************************************/
// Student中的ID屬性對(duì)應(yīng)著字典中的id
// ....
[Student setupReplacedKeyFromPropertyName:^NSDictionary *{
return @{@"ID" : @"id",
@"desc" : @"desciption",
@"oldName" : @"name.oldName",
@"nowName" : @"name.newName",
@"nameChangedTime" : @"name.info.nameChangedTime",
@"bag" : @"other.bag"
};
}];
// 相當(dāng)于在Student.m中實(shí)現(xiàn)了+replacedKeyFromPropertyName方法
NSDictionary *dict = @{
@"id" : @"20",
@"desciption" : @"孩子",
@"name" : @{
@"newName" : @"lufy",
@"oldName" : @"kitty",
@"info" : @{
@"nameChangedTime" : @"2013-08"
}
},
@"other" : @{
@"bag" : @{
@"name" : @"小書包",
@"price" : @100.7
}
}
};
// 將字典轉(zhuǎn)為Student模型
Student *stu = [Student objectWithKeyValues:dict];
// 打印Student模型的屬性
NSLog(@"ID=%@, desc=%@, oldName=%@, nowName=%@, nameChangedTime=%@",
stu.ID, stu.desc, stu.oldName, stu.nowName, stu.nameChangedTime);
// ID=20, desc=孩子, oldName=kitty, nowName=lufy, nameChangedTime=2013-08
NSLog(@"bagName=%@, bagPrice=%f", stu.bag.name, stu.bag.price);
// bagName=小書包, bagPrice=100.700000
核心代碼
調(diào)用+ (void)setupReplacedKeyFromPropertyName:方法
[Student objectWithKeyValues:dict]
將一個(gè)字典數(shù)組轉(zhuǎn)成模型數(shù)組
NSArray *dictArray = @[
@{
@"name" : @"Jack",
@"icon" : @"lufy.png",
},
@{
@"name" : @"Rose",
@"icon" : @"nami.png",
}
];
// 將字典數(shù)組轉(zhuǎn)為User模型數(shù)組
NSArray *userArray = [User objectArrayWithKeyValuesArray:dictArray];
// 打印userArray數(shù)組中的User模型屬性
for (User *user in userArray) {
NSLog(@"name=%@, icon=%@", user.name, user.icon);
}
// name=Jack, icon=lufy.png
// name=Rose, icon=nami.png
核心代碼
[User objectArrayWithKeyValuesArray:dictArray]
將一個(gè)模型轉(zhuǎn)成字典
// 新建模型
User *user = [[User alloc] init];
user.name = @"Jack";
user.icon = @"lufy.png";
Status *status = [[Status alloc] init];
status.user = user;
status.text = @"今天的心情不錯(cuò)烙肺!";
// 將模型轉(zhuǎn)為字典
NSDictionary *statusDict = status.keyValues;
NSLog(@"%@", statusDict);
/*
{
text = "今天的心情不錯(cuò)纳猪!";
user =? ? {
icon = "lufy.png";
name = Jack;
};
}
*/
// 多級(jí)映射的模型
Student *stu = [[Student alloc] init];
stu.ID = @"123";
stu.oldName = @"rose";
stu.nowName = @"jack";
stu.desc = @"handsome";
stu.nameChangedTime = @"2018-09-08";
Bag *bag = [[Bag alloc] init];
bag.name = @"小書包";
bag.price = 205;
stu.bag = bag;
NSDictionary *stuDict = stu.keyValues;
NSLog(@"%@", stuDict);
/*
{
desciption = handsome;
id = 123;
name =? ? {
info =? ? ? ? {
nameChangedTime = "2018-09-08";
};
newName = jack;
oldName = rose;
};
other =? ? {
bag =? ? ? ? {
name = "小書包";
price = 205;
};
};
}
*/
核心代碼
status.keyValues、stu.keyValues
將一個(gè)模型數(shù)組轉(zhuǎn)成字典數(shù)組
// 新建模型數(shù)組
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];
// 將模型數(shù)組轉(zhuǎn)為字典數(shù)組
NSArray *dictArray = [User keyValuesArrayWithObjectArray:userArray];
NSLog(@"%@", dictArray);
/*
(
{
icon = "lufy.png";
name = Jack;
},
{
icon = "nami.png";
name = Rose;
}
)
*/
核心代碼
[User keyValuesArrayWithObjectArray:userArray]
Core Data
NSDictionary *dict = @{
@"name" : @"Jack",
@"icon" : @"lufy.png",
@"age" : @20,
@"height" : @1.55,
@"money" : @"100.9",
@"sex" : @(SexFemale),
@"gay" : @"true"
};
// 這個(gè)Demo僅僅提供思路桃笙,具體的方法參數(shù)需要自己創(chuàng)建
NSManagedObjectContext *context = nil;
User *user = [User objectWithKeyValues:dict context:context];
// 利用CoreData保存模型
[context save:nil];
Core code
[User objectWithKeyValues:dict context:context]
Coding
#import "MJExtension.h"
@implementation User
// NSCoding實(shí)現(xiàn)
MJCodingImplementation
@end
/***********************************************/
// Bag類中的name屬性不參與歸檔
[Bag setupIgnoredCodingPropertyNames:^NSArray *{
return @[@"name"];
}];
// 相當(dāng)于在Bag.m中實(shí)現(xiàn)了+ignoredCodingPropertyNames方法
// 創(chuàng)建模型
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
Core code
MJCodingImplementation
調(diào)用+ (void)setupIgnoredCodingPropertyNames方法(如果全部屬性都要?dú)w檔氏堤、解檔,那就不需要調(diào)用這個(gè)方法)
更多用法
參考NSObject+MJKeyValue.h
參考NSObject+MJCoding.h