苹果客户端接到后台的数据,要转换成对象,然后才能使用。而有的初学者或比较懒的人可能直接那后台放回的结果不进行对象转换直接使用,甚至无论是什么类型的数据都按照字符串处理,使用时再转换为数字等接口定义的类型。若后后台返回的是NSNumber类型,而这种懒惰性写法会直接造成闪退。
最佳的解决方案是:客户端把后台返回数据通过MJExtension第三方库进行对象转换,不直接使用后台数据(self.shopHomeEntity.shopHomeResultHeadEntity = [CBPShopHomeResultHeadEntity mj_objectWithKeyValues:resultDic];)。
当原来的代码已经大量直接使用后台数据(VC.cardtype = self.dataDic[@"shopCardTypeResponse"][@"cardType"];//布尔类型0或1。 if([self.cardType intValue] == 1))。当然最佳解决方案还是重构代码,全部转换为对象后使用。当没有时间重构代码怎么办呢?
有两种临时方案:
1.后台对返回的字符串全部进行二次转换,把键值对的值是NSNumber类型的全部进行二次转换,转换成标准字符串。
2.iOS客户端在网络请求组件底层对接收的结果,扫描键值对的值是否是NSNumber类型,若是就转换成标准字符串:
if([result isKindOfClass:[NSNumber class]]) { return [NSString stringWithFormat:@"%@", result]; }
后台返回的数据一般只有这些类型:
//字典->数组->字典
///字典->字典->数组->字典
//字典->字典->字典
//数组->字典->数组->字典
//数组->数组->字典
//数组->字典->数组->字典
我下面的函数能把这种接口的后台数据中所有的键值对的值是NSNumber类型的全部进行二次转换,转换成标准字符串。
后台返回的数据有的字段是全数字类型的标准字符串,有的是NSNumber类型,如:cardType字段。
后台返回的数据有的字段是全数字类型的标准字符串,有的是NSNumber类型,如:cardType字段。当然字符串类型接受就是这样了。
当然直接if([self.cardType intValue] == 1)这样类型就内存越界了,有可能闪退了。
下面是把后台结果键值对的值是NSNumber类型的全部进行二次转换,转换成标准字符串的函数。使用代码例子:id result= [self modifyNSNumber:data];
-(id)modifyNSNumber:(id)result { if(nil == result) { return result; } else if([result isKindOfClass:[NSNull class]]) { return nil; } else if([result isKindOfClass:[NSNumber class]]) { return [NSString stringWithFormat:@"%@", result]; } else if([result isKindOfClass:[NSString class]]) { return result; } else if([result isKindOfClass:[NSDictionary class]]) { NSMutableDictionary *resultDic = [NSMutableDictionary dictionary]; NSDictionary *dic1 = (NSDictionary *)result; for(NSInteger i = 0;i <dic1.allKeys.count; i++) { NSString *key1 = dic1.allKeys[i]; NSString *value1 = dic1.allValues[i]; // if([value1 isKindOfClass:[NSNull class]]) if(nil == value1) { //不存在防范式编程 } else if([value1 isKindOfClass:[NSNull class]]) { [resultDic setValue:value1 forKey:key1]; } else if([value1 isKindOfClass:[NSString class]]) { [resultDic setValue:value1 forKey:key1]; } else if([value1 isKindOfClass:[NSNumber class]]) { [resultDic setValue:[NSString stringWithFormat:@"%@", value1] forKey:key1]; } else if([value1 isKindOfClass:[NSArray class]]) { NSArray *arr3 = (NSArray *)value1;//2层 NSMutableArray *arr4 = [NSMutableArray array]; for(NSInteger i3 = 0;i3 <arr3.count; i3++) { id value5 = arr3[i3];//第3层 if(nil == value5) { //不存在,防范式编程 } else if([value5 isKindOfClass:[NSNull class]]) { [arr4 addObject:value5]; } else if([value5 isKindOfClass:[NSNumber class]]) { [arr4 addObject:[NSString stringWithFormat:@"%@", value5]]; } else if([value5 isKindOfClass:[NSString class]]) { [arr4 addObject:value5]; } else if([value5 isKindOfClass:[NSDictionary class]]) { NSMutableDictionary *resultDic6 = [NSMutableDictionary dictionary];//第4层 NSDictionary *dic5 = (NSDictionary *)value5; for(NSInteger i6 = 0;i6 <dic5.allKeys.count; i6++) { NSString *key7 = dic5.allKeys[i6];//第七层 NSString *value7 = dic5.allValues[i6]; if(nil == value5) { //不存在,防范式编程 } else if([value5 isKindOfClass:[NSNull class]]) { [resultDic6 setValue:value7 forKey:key7]; } else if([value7 isKindOfClass:[NSNumber class]]) { [resultDic6 setValue:[NSString stringWithFormat:@"%@", value7] forKey:key7]; } else if([value5 isKindOfClass:[NSString class]]) { [resultDic6 setValue:value7 forKey:key7]; } else { [resultDic6 setValue:value7 forKey:key7]; } } [arr4 addObject:resultDic6]; } } [resultDic setValue:arr4 forKey:key1];//二层 }//字典-》数组-〉字典 else if([value1 isKindOfClass:[NSDictionary class]]) { NSMutableDictionary *resultDic2 = [NSMutableDictionary dictionary];//二层 NSDictionary *dic2 = (NSDictionary *)value1; for(NSInteger i2 = 0;i2 <dic2.allKeys.count; i2++) { NSString *key2 = dic2.allKeys[i2];//三层 NSString *value2 = dic2.allValues[i2]; if(nil == value2) { //不存在防范式编程 } else if([value2 isKindOfClass:[NSNull class]]) { [resultDic2 setValue:value2 forKey:key2]; } else if([value2 isKindOfClass:[NSNumber class]]) { [resultDic2 setValue:[NSString stringWithFormat:@"%@", value1] forKey:key1]; } else if([value2 isKindOfClass:[NSString class]]) { [resultDic2 setValue:value2 forKey:key2]; } else if([value2 isKindOfClass:[NSArray class]]) { NSArray *arr3 = (NSArray *)value2;//四层 NSMutableArray *arr4 = [NSMutableArray array]; for(NSInteger i3 = 0;i3 <arr3.count; i3++) { id value5 = arr3[i3];//第五层 if(nil == value5) { //不存在,防范式编程 } else if([value5 isKindOfClass:[NSNull class]]) { [arr4 addObject:value5]; } else if([value5 isKindOfClass:[NSNumber class]]) { [arr4 addObject:[NSString stringWithFormat:@"%@", value5]]; } else if([value5 isKindOfClass:[NSString class]]) { [arr4 addObject:value5]; } else if([value5 isKindOfClass:[NSDictionary class]]) { NSMutableDictionary *resultDic6 = [NSMutableDictionary dictionary];//第六层 NSDictionary *dic5 = (NSDictionary *)value5; for(NSInteger i6 = 0;i6 <dic5.allKeys.count; i6++) { NSString *key7 = dic5.allKeys[i6];//第七层 NSString *value7 = dic5.allValues[i6]; if(nil == value5) { //不存在,防范式编程 } else if([value5 isKindOfClass:[NSNull class]]) { [resultDic6 setValue:value7 forKey:key7]; } else if([value7 isKindOfClass:[NSNumber class]]) { [resultDic6 setValue:[NSString stringWithFormat:@"%@", value7] forKey:key7]; } else if([value5 isKindOfClass:[NSString class]]) { [resultDic6 setValue:value7 forKey:key7]; } else { [resultDic6 setValue:value7 forKey:key7]; } } [arr4 addObject:resultDic6]; } } [resultDic2 setValue:arr4 forKey:key2]; }//字典-》字典-〉数组-》字典 else if([value2 isKindOfClass:[NSDictionary class]]) { NSMutableDictionary *resultDic6 = [NSMutableDictionary dictionary];//第4层 NSDictionary *dic5 = (NSDictionary *)value2; for(NSInteger i6 = 0;i6 <dic5.allKeys.count; i6++) { NSString *key7 = dic5.allKeys[i6];//第5层 NSString *value7 = dic5.allValues[i6]; if(nil == value7) { //不存在,防范式编程 } else if([value7 isKindOfClass:[NSNull class]]) { [resultDic6 setValue:value7 forKey:key7]; } else if([value7 isKindOfClass:[NSNumber class]]) { [resultDic6 setValue:[NSString stringWithFormat:@"%@", value7] forKey:key7]; } else if([value7 isKindOfClass:[NSString class]]) { [resultDic6 setValue:value7 forKey:key7]; } else { [resultDic6 setValue:value7 forKey:key7]; } } [resultDic2 setValue:resultDic6 forKey:key2]; }//字典-》字典-〉字典 else { [resultDic2 setValue:value1 forKey:key1];//二层 } } [resultDic setValue:resultDic2 forKey:key1];//z 二层 } else { [resultDic setValue:value1 forKey:key1]; } } return resultDic; } else if([result isKindOfClass:[NSArray class]]) { NSArray *arr1 = (NSArray *)result; NSMutableArray *arr2 = [NSMutableArray array];//一层 for(NSInteger i1 = 0;i1 <arr1.count; i1++) { id value1 = arr1[i1]; if(nil == value1) { //不存在防范式编程 } else if([value1 isKindOfClass:[NSNull class]]) { [arr2 addObject:value1]; } else if([value1 isKindOfClass:[NSNumber class]]) { [arr2 addObject:[NSString stringWithFormat:@"%@", value1]]; } else if([value1 isKindOfClass:[NSString class]]) { [arr2 addObject:value1]; } else if([value1 isKindOfClass:[NSDictionary class]]) { NSMutableDictionary *resultDic2 = [NSMutableDictionary dictionary];//二层 NSDictionary *dic2 = (NSDictionary *)value1; for(NSInteger i2 = 0;i2 <dic2.allKeys.count; i2++) { NSString *key2 = dic2.allKeys[i2];//三层 NSString *value2 = dic2.allValues[i2]; if(nil == value2) { //不存在防范式编程 } else if([value2 isKindOfClass:[NSNull class]]) { [resultDic2 setValue:value2 forKey:key2]; } else if([value2 isKindOfClass:[NSNumber class]]) { [resultDic2 setValue:[NSString stringWithFormat:@"%@", value2] forKey:key2]; } else if([value2 isKindOfClass:[NSString class]]) { [resultDic2 setValue:value2 forKey:key2]; } else if([value2 isKindOfClass:[NSArray class]]) { NSArray *arr3 = (NSArray *)value2;//四层 NSMutableArray *arr4 = [NSMutableArray array]; for(NSInteger i3 = 0;i3 <arr3.count; i3++) { id value5 = arr3[i3];//第五层 if(nil == value5) { //不存在,防范式编程 } else if([value5 isKindOfClass:[NSNull class]]) { [arr4 addObject:value5]; } else if([value5 isKindOfClass:[NSNumber class]]) { [arr4 addObject:[NSString stringWithFormat:@"%@", value5]]; } else if([value5 isKindOfClass:[NSString class]]) { [arr4 addObject:value5]; } else if([value5 isKindOfClass:[NSDictionary class]]) { NSMutableDictionary *resultDic6 = [NSMutableDictionary dictionary];//第六层 NSDictionary *dic6 = (NSDictionary *)value5; for(NSInteger i6 = 0;i6 <dic6.allKeys.count; i6++) { NSString *key7 = dic6.allKeys[i6];//第七层 NSString *value7 = dic6.allValues[i6]; if(nil == value7) { //不存在,防范式编程 } else if([value7 isKindOfClass:[NSNull class]]) { [resultDic6 setValue:value7 forKey:key7]; } else if([value7 isKindOfClass:[NSNumber class]]) { [resultDic6 setValue:[NSString stringWithFormat:@"%@", value7] forKey:key7]; } else if([value7 isKindOfClass:[NSString class]]) { [resultDic6 setValue:value7 forKey:key7]; } else { [resultDic6 setValue:value7 forKey:key7]; } } [arr4 addObject:resultDic6]; } } [resultDic2 setValue:arr4 forKey:key2]; }//数组-》字典-〉数组-》字典 else { [resultDic2 setValue:value2 forKey:key2];//二层 } } [arr2 addObject:resultDic2];//二层 } else if([value1 isKindOfClass:[NSArray class]]) { NSArray *arr3 = (NSArray *)value1;//2层 NSMutableArray *arr4 = [NSMutableArray array]; for(NSInteger i3 = 0;i3 <arr3.count; i3++) { id value5 = arr3[i3];//第3层 if(nil == value5) { //不存在,防范式编程 } else if([value5 isKindOfClass:[NSNull class]]) { [arr4 addObject:value5]; } else if([value5 isKindOfClass:[NSNumber class]]) { [arr4 addObject:[NSString stringWithFormat:@"%@", value5]]; } else if([value5 isKindOfClass:[NSString class]]) { [arr4 addObject:value5]; } else if([value5 isKindOfClass:[NSDictionary class]]) { NSMutableDictionary *resultDic6 = [NSMutableDictionary dictionary];//第4层 NSDictionary *dic5 = (NSDictionary *)value5; for(NSInteger i6 = 0;i6 <dic5.allKeys.count; i6++) { NSString *key7 = dic5.allKeys[i6];//第5层 NSString *value7 = dic5.allValues[i6]; if(nil == value7) { //不存在,防范式编程 } else if([value7 isKindOfClass:[NSNull class]]) { [resultDic6 setValue:value7 forKey:key7]; } else if([value7 isKindOfClass:[NSNumber class]]) { [resultDic6 setValue:[NSString stringWithFormat:@"%@", value7] forKey:key7]; } else if([value7 isKindOfClass:[NSString class]]) { [resultDic6 setValue:value7 forKey:key7]; } else { [resultDic6 setValue:value7 forKey:key7]; } } [arr4 addObject:resultDic6]; } } [arr2 addObject:arr4];//二层 }//数组-》数组-〉字典 else { [arr2 addObject:value1];//二层 } } return arr2;//一层返回 数组-》字典-〉数组-》字典 } else { return result; } }