swift - MJExtension 使用

罗建弼
2023-12-01
MJExtension 简介:(建议使用object-c语言的model,swift的原因之后进行解释)
MJExtension 是一套字典与模型直接进行相互转化的超轻量级框架,可以使用 MJExtension 处理以下的模型字典转换
1、json -> model 、 core data model
2、jsonString -> model、core data model
3、model、 core data model -> json
4、json array -> model array、core data model array
5、jsonString -> model array、core data model array
6、model array、core data model array -> json array
7、模型之间的归档与解规定,用于数据存储
/*一个简单的model使用*/
@interface UserModel : NSObject
@property (copy, nonatomic) NSString *name;
@property (copy, nonatomic) NSString *icon;
@property (assign, nonatomic) int age;
@property (copy, nonatomic) NSString *height;
@end
func simpleUser() -> Void {
let userDic:Dictionary<String,Any> = ["name":"Jack","icon":"lufy.png",
"age":20,"height":"1.75"]
let userModel:UserModel = UserModel.mj_object(withKeyValues: userDic)
print("userModel\(userModel.name, userModel.icon, userModel.age, userModel.height)")
}
一个简单的小补充:解析常常配合缓存进行使用,当我们对自定义的model进行缓存时需要进行归档与解归档操作。使用MJExtension两步就可以完成归档与解归档
1、引入MJExtension
2、输入MJCodingImplementation
归档与解归档时的常用选择
// 在进行归档时忽略返回的属性数组
1、UserModel.mj_setupIgnoredCodingPropertyNames { () -> [Any]? in
}
// 在进行归档时仅进行归档返回的属性数组
2、UserModel.mj_setupAllowedCodingPropertyNames { () -> [Any]? in

}
字典model转换时我们经常对model进行处理(配置黑白名单)
1、mj_setupIgnoredPropertyNames:(MJIgnoredPropertyNames)ignoredPropertyNames; 设置黑名单
2、mj_totalIgnoredPropertyNames 设置黑名单
3、mj_setupAllowedCodingPropertyNames:(MJAllowedCodingPropertyNames)allowedCodingPropertyNames; 设置白名单
4、mj_totalAllowedCodingPropertyNames 设置白名单

/*当model中含有model时的解析操作*/
#import <Foundation/Foundation.h>
#import "UserModel.h"
@interface PersonModel : NSObject
@property (nonatomic,strong) UserModel *user;
@property (nonatomic,copy) NSString *address;
@property (nonatomic,copy) NSString *infoMation;
@end
func contentModel() -> Void {
let personDic:Dictionary<String,Any> = ["address":"上海市","infoMation":"你还能说什么","user":["name":"Jack","height":"1.75"]]
let personModel:PersonModel = PersonModel.mj_object(withKeyValues: personDic)
print("personMode:\(personModel.address, personModel.infoMation,personModel.user.name,personModel.user.height,personModel.user.age)")
let cache:YYCache = YYCache(name: "PersonCache")!
cache.setObject(personModel as? NSCoding, forKey: "personKey")
}
/*当model中含有数组时,数组中有的是model,有的是非model时的操作*/
#import <Foundation/Foundation.h>
#import "UserModel.h"
@interface ArrayModel : NSObject
@property (nonatomic,strong) NSMutableArray *presonModelArray;
// 当存放的不是model时接受数组
@property (nonatomic,strong) NSMutableArray *other;
@property (nonatomic,copy) NSString *descripution;
@end
func contentArray() -> Void {
/*因为arrayModel中含有数组,所以需要进行类型指定操作*/
/*数组中需要转换的模型类*/
ArrayModel.mj_setupObjectClass { () -> [AnyHashable : Any]? in
return ["presonModelArray":"PersonModel"]
}
let arrayDic:Dictionary<String,Any> = ["presonModelArray":[
["address":"上海市","infoMation":"你还能说什么","user":["name":"Jack","height":"1.75"]],
["address":"上海市","infoMation":"你还能说什么","user":["name":"Jack","height":"1.75"]],
["address":"上海市","infoMation":"你还能说什么","user":["name":"Jack","height":"1.75"]]],
"other":["name","sex","age","other"],
"descripution":"lalalala"]
let arrayModel:ArrayModel = ArrayModel.mj_object(withKeyValues: arrayDic)
print("arrayModel:\(arrayModel.presonModelArray, arrayModel.descripution, arrayModel.other)")
}

/*模型属性名与字典中的key不同时的操作*/
#import <Foundation/Foundation.h>
@interface DefirentModel : NSObject
@property (copy, nonatomic) NSString *def;
@property (copy, nonatomic) NSString *desc;
@property (copy, nonatomic) NSString *nowName;
@property (copy, nonatomic) NSString *oldName;
@property (copy, nonatomic) NSString *nameChangedTime;
@end
func defirentModel() -> Void {
let defDic:Dictionary<String,Any> = ["id":"20",
"name":["newName":"lufy",
"oldName":"kitty",
"info":["test-data",["nameChangedTime":"2013-08"]]],
"desciption":"kids",
"other":["bag":["name":"a red bag","price":"100.7"]]]
/*字典属性名称映射
* 把字典中的key映射到model中
*/
DefirentModel.mj_setupReplacedKey { () -> [AnyHashable : Any]? in
return ["def":"id",
"desc":"desciption",
"oldName":"name.oldName",
"nowName":"name.newName",
"nameChangedTime":"name.info[1].nameChangedTime",
"bag":"other.bag"]
}
let model:DefirentModel = DefirentModel.mj_object(withKeyValues: defDic)
print("model:\(model.def,model.desc,model.oldName,model.nowName,model.nameChangedTime)")
}
/*数组字典转换成模型
* 前提条件是数组中存储的是同一类的内容,
*/
/*数组字典转换成模型*/
func changeArrayForModeArray() -> Void {
let userArray:Array<Any> = [["name":"Jack","icon":"lufy.png",
"age":20,"height":"1.75"],
["name":"Jack","icon":"lufy.png",
"age":20,"height":"1.75"],
["name":"Jack","icon":"lufy.png",
"age":20,"height":"1.75"],
["name":"Jack","icon":"lufy.png",
"age":20,"height":"1.75"]]
let array:Array<UserModel> = UserModel.mj_objectArray(withKeyValuesArray: userArray) as! Array<UserModel>
print("array:\(array)")
}
 类似资料: