MJExtension是JSON和模型之間最快捷方便的轉換iOS框架。
最近壹次測試表明:MJExtension>JSONModel>Mantle
各位開發者也可以自行測試
具體用法:
JSONModel:要求所有模型類必須繼承自JSONModel基類
Mantle:要求所有模型類必須繼承自MTModel基類
MJExtension:不需要妳的模型類繼承任何特殊基類,毫無汙染,毫無侵入性
MJExtension是壹套字典和模型之間互相轉換的超輕量級框架
MJExtension能完成的功能
字典(JSON)-->模型(Model)
模型(Model)-->字典(JSON)
字典數組(JSON Array)-->模型數組(Model Array)
模型數組(Model Array)-->字典數組(JSON Array)
詳盡用法主要參考 main.m中的各個函數 以及NSObject+MJKeyValue.h
回到頂部
回到頂部
如何使用MJExtension
cocoapods導入:pod 'MJExtension'
手動導入:
將MJExtensionExample/MJExtensionExample/MJExtension文件夾中的所有源代碼拽入項目中
導入主頭文件:#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
回到頂部
最簡單的字典轉模型
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
核心代碼