ModelToDictionary 1.1.7

ModelToDictionary 1.1.7

yangYue3 维护。



  • POSUN-MAC

ModelToDictionary

前言

在实际项目开发中,将 Model 对象数据转换成 NSDictionary 对象(或包含 NSDictionary 对象的数组),然后通过接口提交给服务器是开发中经常要做的事情。然而,手动拼写 NSDictionary 对象既麻烦又容易出错。因此,我编写了这样一个功能,可以将 Model 对象数据转换成 NSDictionary 对象。并且可以重写 Model 类方法来重命名生成的字典的 key 以及可以屏蔽掉一些在生成 NSDictionary 对象中不需要的键值对。


1.只需在项目中引入 NSObject+Dictionary 分类,然后调用“-(NSDictionary *)toDictionary”方法。例如:

//给test对象赋值(注意填写正确的文件路径)
NSString *str=[NSString stringWithContentsOfFile:@"/Users/POSUN/Documents/ModelToDictionary/ModelToDictionary/TestJson.json" encoding:NSUTF8StringEncoding error:nil];

if (str == nil){return;}
NSData *jsonData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSError *error=nil;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&error];
TEST *test=[[TEST alloc] init];
[test setValuesForKeysWithDictionary:dic];

//将test对象转换传NSDictionary对象
NSDictionary *testDic=[test toDictionary];
NSLog("%@",testDic);

运行结果:

2018-07-16 17:25:07.007223+0800 ModelToDictionary[3678:582786] {
salesOrder =     {
assistant1 = xxx;
assistantId = 000001;
deliveryType = Y;
deliveryTypeName = "\U9001\U8d27\U4e0a\U95e8";
needInstall = Y;
number = 4534;
orderDate = 1525507xxx;
orgId = A;
orgName = "xxx\U6709\U9650\U516c\U53f8";
priceSum = "3.000000";
refundType = 1;
requireArriveDate = 1525593xxx;
salesOrderParts =         {
billPrice = "3.00";
goods =             {
Id = "";
enableSn = "";
giftFlag = "";
goodsTypeName = "";
partName = "xxxCFXB50YB8-70//4";
pnModel = 5L;
serialRuleId = "";
};
partRecId = "01.01.01.400050";
priceRate = "0.000000";
qtyPlan = 1;
unitId = tai;
unitName = "\U53f0";
unitPrice = 3;
};
subscribeDispatch = N;
warehouseId = 1013;
warehouseName = "xxx\U5e93";
};
salesRefund =     {
assistant1 = xxx;
assistantId = 000001;
buyerId = QB027;
buyerName = xxx;
orderDate = 1525507xxx;
orgId = A;
orgName = "xxx\U6709\U9650\U516c\U53f8";
priceSum = "3.000000";
refundType = 1;
requireArriveDate = 1525593xxx;
salesOrderParts =         {
billPrice = "3.00";
goods =             {
Id = "";
enableSn = "";
goodsTypeId = "";
goodsTypeName = "";
partName = "xxxCFXB40YB8-70//4";
pnModel = 4L;
serialRuleId = "";
unitId = "";
unitName = "";
};
partRecId = "01.01.01.400043";
priceRate = "0.000000";
qtyPlan = 1;
unitId = tai;
unitName = "\U53f0";
unitPrice = 3;
};
warehouseId = 1011;
warehouseName = "xxx\U5e93";
};
testDouble = 0;
}

2.重置生成 NSdictionary 对象的 Key,需要在对应的 Model 类中重写“-(NSDictionary*)YYMTD_ResetKeyDictionary”例如:

#import "TEST.h"
@implementation TEST
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
NSLog(@"UndefinedKey:%@",key);
};
-(void)setValue:(id)value forKey:(NSString *)key{
if([key isEqualToString:@"salesRefund"]){
self.salesRefund=[[SalesRefund alloc] init];
[self.salesRefund setValuesForKeysWithDictionary:value];
}else if([key isEqualToString:@"salesOrder"]){
self.salesOrder=[[SalesOrder alloc] init];
[self.salesOrder setValuesForKeysWithDictionary:value];
}else{
[super setValue:value forKey:key];
}
}

-(NSDictionary*)YYMTD_ResetKeyDictionary{
return @{
@"salesOrder":@"SO",
};
}
@end

然后调用“-(NSDictionary *)toDictionary”,运行结果:

2018-07-16 17:26:12.677741+0800 ModelToDictionary[3699:584637] {
SO =     {
assistant1 = xxx;
assistantId = 000001;
deliveryType = Y;
deliveryTypeName = "\U9001\U8d27\U4e0a\U95e8";
needInstall = Y;
number = 4534;
orderDate = 1525507xxx;
orgId = A;
orgName = "xxx\U6709\U9650\U516c\U53f8";
priceSum = "3.000000";
refundType = 1;
requireArriveDate = 1525593xxx;
salesOrderParts =         {
billPrice = "3.00";
goods =             {
Id = "";
enableSn = "";
giftFlag = "";
goodsTypeName = "";
partName = "xxxCFXB50YB8-70//4";
pnModel = 5L;
serialRuleId = "";
};
partRecId = "01.01.01.400050";
priceRate = "0.000000";
qtyPlan = 1;
unitId = tai;
unitName = "\U53f0";
unitPrice = 3;
};
subscribeDispatch = N;
warehouseId = 1013;
warehouseName = "xxx\U5e93";
};
salesRefund =     {
assistant1 = xxx;
assistantId = 000001;
buyerId = QB027;
buyerName = xxx;
orderDate = 1525507xxx;
orgId = A;
orgName = "xxx\U6709\U9650\U516c\U53f8";
priceSum = "3.000000";
refundType = 1;
requireArriveDate = 1525593xxx;
salesOrderParts =         {
billPrice = "3.00";
goods =             {
Id = "";
enableSn = "";
goodsTypeId = "";
goodsTypeName = "";
partName = "xxxCFXB40YB8-70//4";
pnModel = 4L;
serialRuleId = "";
unitId = "";
unitName = "";
};
partRecId = "01.01.01.400043";
priceRate = "0.000000";
qtyPlan = 1;
unitId = tai;
unitName = "\U53f0";
unitPrice = 3;
};
warehouseId = 1011;
warehouseName = "xxx\U5e93";
};
testDouble = 0;
}

可以看到原本 key 为 salesOrder 的被修改成了 SO。

3.生成 NSdictionary 时去掉不需要的键值对,需要在对应的 Model 类中重写“-(NSSet*)YYMTD_UnconversionProperty”例如:

#import "TEST.h"
@implementation TEST
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
NSLog(@"UndefinedKey:%@",key);
};
-(void)setValue:(id)value forKey:(NSString *)key{
if([key isEqualToString:@"salesRefund"]){
self.salesRefund=[[SalesRefund alloc] init];
[self.salesRefund setValuesForKeysWithDictionary:value];
}else if([key isEqualToString:@"salesOrder"]){
self.salesOrder=[[SalesOrder alloc] init];
[self.salesOrder setValuesForKeysWithDictionary:value];
}else{
[super setValue:value forKey:key];
}
}

-(NSSet *)YYMTD_UnconversionProperty{
NSSet *set=[[NSSet alloc] initWithObjects:@"salesRefund", nil];
return set;
}
@end

然后调用“-(NSDictionary *)toDictionary”,运行结果:

2018-07-16 17:31:21.647170+0800 ModelToDictionary[3745:592039] {
salesOrder =     {
assistant1 = xxx;
assistantId = 000001;
deliveryType = Y;
deliveryTypeName = "\U9001\U8d27\U4e0a\U95e8";
needInstall = Y;
number = 4534;
orderDate = 1525507xxx;
orgId = A;
orgName = "xxx\U6709\U9650\U516c\U53f8";
priceSum = "3.000000";
refundType = 1;
requireArriveDate = 1525593xxx;
salesOrderParts =         {
billPrice = "3.00";
goods =             {
Id = "";
enableSn = "";
giftFlag = "";
goodsTypeName = "";
partName = "xxxCFXB50YB8-70//4";
pnModel = 5L;
serialRuleId = "";
};
partRecId = "01.01.01.400050";
priceRate = "0.000000";
qtyPlan = 1;
unitId = tai;
unitName = "\U53f0";
unitPrice = 3;
};
subscribeDispatch = N;
warehouseId = 1013;
warehouseName = "xxx\U5e93";
};
testDouble = 0;
}

可以看到生成的 NSDictionary 里 salesRefund 被去掉了。

项目地址:https://github.com/pousniOS/ModelToDictionary