iOS将dict 转换成model,框架不少了,这样大大的解决了我们开发的效率了。但是,如果反过来呢,将model转换字典怎么办,一个一个的手动去写吧,特别是在一些复杂的request请求当中,model,嵌套model,以及一些复杂的数据存储,要将model转化成json字符存 储,怎么办?
个人在工作中因复杂的请求,创建了一个小小的QMrequest分类,因为是从request出发创建的分类,所以命名,也懒得改了。用户和mj_extion很像,使用起来简单粗暴。model 中,支持使用int double nstimerintval 等等类型. int,double 类型的值是 0 的时候,不会包装到字典当中的.
忽略数组,当前类中还有的其他class类,以及replace替换数组.默认是不管继承多少层model,利用递归思想,一层一层的从父类中去找去找属性,然后,转化成字典,可以通过设置 isNotnendParentProperty 去设置,自找当前model的属性。核心代码
-(void)getPropertyListOfClass:(Class)zlass{
// NSLog(@"zlass --%@",zlass);
if(![NSStringFromClass(zlass) isEqualToString:@"NSObject"]) {
// NSLog(@"Subclass --%@",zlass);
unsignedintcount=0;
objc_property_t *propertys = class_copyPropertyList(zlass, &count);
for (int i=0; i
objc_property_t property =propertys[i];
NSString* propertyName = [NSString stringWithCString: property_getName(property) encoding:NSUTF8StringEncoding];
if(![selfisNotNeedParentProperty]) {
[propertysArr addObject:propertyName];
}
}
[selfgetPropertyListOfClass:[zlass superclass]];
}else{
NSLog(@"这是最顶级的父类了NSObject ");
}
}
-(NSDictionary*)getDictFrommemberList{
if(propertysArr==nil) {
propertysArr=[[NSMutableArray alloc]init];
}
[propertysArr removeAllObjects];
[selfgetPropertyListOfClass:[selfclass]];
NSMutableDictionary*Propertydict=[[NSMutableDictionary alloc]init];
for(inti=0; i
NSString*propertyName=propertysArr[i];
// 忽略数组
if([selfingoreArray]&&[selfingoreArray].count>0) {//忽略数组
if([[selfingoreArray] containsObject:propertyName]) {
continue;
}
}
idvalue =[selfvalueForKeyPath:propertyName];
if([selfotherClassesInClassArray]) {//含有其他的类
NSDictionary*dict=[selfotherClassesInClassArray];
for(NSString*keyindict.allKeys) {
if([propertyName isEqualToString:key]) {//
if(!value) {
break;
}
idobj=[selfgetObjectWithpropertyName:propertyName];
if([obj isKindOfClass:[NSArray class]]) {//数组
NSMutableArray*arrayM=[NSMutableArray array];
for(idclassObjinobj) {
[arrayM addObject: [classObj getDictFrommemberList]];
}
obj=arrayM;
value=arrayM;
}elseif([obj isKindOfClass:[NSDictionary class]]){//字典
NSMutableDictionary*dictM=[NSMutableDictionary dictionary];
// [dict set]
for(NSString*subkeyin((NSDictionary*)obj).allKeys) {
[dictM setObject:[((NSDictionary*)obj)[subkey] getDictFrommemberList] forKey:subkey];
}
obj=dict;
value=dictM;
}
else{//普通的类
value=[obj getDictFrommemberList];
}
break;// 匹配上了 for (NSString*key in dict.allKeys) {循环终止,去匹配下一个propertyName
}
else{//属性不是class的普通的属性
value = [selfvalueForKeyPath:propertyName];//普通的属性
}
}
}else{
NSLog(@"propertyName--%@",propertyName);
value = [selfvalueForKeyPath:propertyName];
}
if(!value){
continue;
}
else if(( [value isKindOfClass:[NSNumber class]]&&[value intValue]==0)){
continue;
}
// else if( [value isKindOfClass:[NSArray class]]&&((NSArray*)value).count==0){
// continue;
// }
// else if ([value isKindOfClass:[NSDictionary class]]&&((NSDictionary*)value).count==0){
// continue;
// }
else{
NSDictionary*replacedict = [selfreplacePropertyWithKey];
if(replacedict.count>0) {
for(NSString*keyinreplacedict.allKeys) {
if([propertyName isEqualToString:key]) {
propertyName=replacedict[key];
break;
}
}
}
[Propertydict setValue:value forKey:propertyName];
}
}
returnPropertydict;
}
-(id)getObjectWithpropertyName:(NSString*)name{
SELselector =NSSelectorFromString(name);
IMPimp=[selfmethodForSelector:selector];
id(*func)(id,SEL)=(void*)imp;
idobj =func(self,selector);
returnobj;
}
有需要的可以看 demo 地址:model自动转化字典的demo