Convert the value of key value pair to NSNumber type into standard string

Posted by SharkBait on Wed, 26 Jan 2022 11:27:14 +0100

The apple client receives the data from the background and needs to convert it into an object before it can be used. Some beginners or lazy people may directly use the results put back in the background without object conversion. Even no matter what type of data is processed according to string, it will be converted to the types defined by interfaces such as numbers. If NSNumber type is returned in the background, and this lazy writing method will directly cause flash back.
The best solution is: the client converts the background returned data through the mjextense third-party library without directly using the background data (self.shopHomeEntity.shopHomeResultHeadEntity = [CBPShopHomeResultHeadEntity mj_objectWithKeyValues:resultDic];).
When the original code has used a lot of background data directly (VC.cardtype = self.dataDic[@"shopCardTypeResponse"][@"cardType"]; / / boolean type 0 or 1. if([self.cardType intValue] == 1)). Of course, the best solution is to refactor the code, convert it all into objects and use it. What happens when there is no time to refactor the code?
There are two temporary schemes:
1. The background performs secondary conversion on all the returned strings, and converts all the values of key value pairs of NSNumber type into standard strings.
2. The IOS client scans whether the value of the key value pair is NSNumber type for the results received at the bottom of the network request component. If so, it will be converted to a standard string:

if([result isKindOfClass:[NSNumber class]])
    {
        return [NSString stringWithFormat:@"%@", result];
    }

Generally, only these types of data are returned in the background:
//Dictionary - > array - > dictionary
///Dictionary - > Dictionary - > array - > dictionary
//Dictionary - > Dictionary - > dictionary
//Array - > Dictionary - > array - > dictionary
//Array - > array - > dictionary
//Array - > Dictionary - > array - > dictionary
The following function can convert the values of all key value pairs in the background data of this interface to NSNumber type twice and convert them into standard strings.


Some fields of the data returned in the background are standard strings of all digital type, and some are NSNumber type, such as cardType field.


Some fields of the data returned in the background are standard strings of all digital type, and some are NSNumber type, such as cardType field. Of course, that's what string type acceptance is.

Of course, direct if([self.cardType intValue] == 1) so that the type memory is out of bounds and may flash back.

The following is a function that converts all the values of the background result key value pair into NSNumber type to standard string. Use code example: 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)
            {
                //There is no preventive programming
            }
            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 floors
                NSMutableArray *arr4 = [NSMutableArray array];
                for(NSInteger i3 = 0;i3 <arr3.count; i3++)
                {
                    id value5 = arr3[i3];//3rd floor
                    if(nil == value5)
                    {
                        //No, preventive programming
                    }
                    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];//4th floor
                        NSDictionary *dic5 = (NSDictionary *)value5;
                        for(NSInteger i6 = 0;i6 <dic5.allKeys.count; i6++)
                        {
                            NSString *key7 = dic5.allKeys[i6];//Seventh floor
                            NSString *value7 = dic5.allValues[i6];
                            if(nil == value5)
                            {
                                //No, preventive programming
                            }
                            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];//Second floor
            }//Dictionary array dictionary
            else if([value1 isKindOfClass:[NSDictionary class]])
            {
                NSMutableDictionary *resultDic2 = [NSMutableDictionary dictionary];//Second floor
                NSDictionary *dic2 = (NSDictionary *)value1;
                for(NSInteger i2 = 0;i2 <dic2.allKeys.count; i2++)
                {
                    NSString *key2 = dic2.allKeys[i2];//three layers
                    NSString *value2 = dic2.allValues[i2];
                    if(nil == value2)
                    {
                        //There is no preventive programming
                    }
                    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;//four layers
                        NSMutableArray *arr4 = [NSMutableArray array];
                        for(NSInteger i3 = 0;i3 <arr3.count; i3++)
                        {
                            id value5 = arr3[i3];//Fifth floor
                            if(nil == value5)
                            {
                                //No, preventive programming
                            }
                            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];//Sixth floor
                                NSDictionary *dic5 = (NSDictionary *)value5;
                                for(NSInteger i6 = 0;i6 <dic5.allKeys.count; i6++)
                                {
                                    NSString *key7 = dic5.allKeys[i6];//Seventh floor
                                    NSString *value7 = dic5.allValues[i6];
                                    if(nil == value5)
                                    {
                                        //No, preventive programming
                                    }
                                    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];
                    }//Dictionary - > Dictionary - > array - > dictionary
                    else if([value2 isKindOfClass:[NSDictionary class]])
                    {
                        NSMutableDictionary *resultDic6 = [NSMutableDictionary dictionary];//4th floor
                        NSDictionary *dic5 = (NSDictionary *)value2;
                        for(NSInteger i6 = 0;i6 <dic5.allKeys.count; i6++)
                        {
                            NSString *key7 = dic5.allKeys[i6];//5th floor
                            NSString *value7 = dic5.allValues[i6];
                            if(nil == value7)
                            {
                                //No, preventive programming
                            }
                            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];
                    }//Dictionary dictionary dictionary
                    else
                    {
                        [resultDic2 setValue:value1 forKey:key1];//Second floor
                    }
                }
                [resultDic setValue:resultDic2 forKey:key1];//z second floor
            }
            else
            {
                [resultDic setValue:value1 forKey:key1];
            }
        }
        return  resultDic;
    }
    else if([result isKindOfClass:[NSArray class]])
    {
        NSArray *arr1 = (NSArray *)result;
        NSMutableArray *arr2 = [NSMutableArray array];//First floor
        for(NSInteger i1 = 0;i1 <arr1.count; i1++)
        {
            id value1 = arr1[i1];
            if(nil == value1)
            {
                //There is no preventive programming
            }
            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];//Second floor
                NSDictionary *dic2 = (NSDictionary *)value1;
                for(NSInteger i2 = 0;i2 <dic2.allKeys.count; i2++)
                {
                    NSString *key2 = dic2.allKeys[i2];//three layers
                    NSString *value2 = dic2.allValues[i2];
                    if(nil == value2)
                    {
                        //There is no preventive programming
                    }
                    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;//four layers
                        NSMutableArray *arr4 = [NSMutableArray array];
                        for(NSInteger i3 = 0;i3 <arr3.count; i3++)
                        {
                            id value5 = arr3[i3];//Fifth floor
                            if(nil == value5)
                            {
                                //No, preventive programming
                            }
                            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];//Sixth floor
                                NSDictionary *dic6 = (NSDictionary *)value5;
                                for(NSInteger i6 = 0;i6 <dic6.allKeys.count; i6++)
                                {
                                    NSString *key7 = dic6.allKeys[i6];//Seventh floor
                                    NSString *value7 = dic6.allValues[i6];
                                    if(nil == value7)
                                    {
                                        //No, preventive programming
                                    }
                                    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];
                    }//Array dictionary array dictionary
                    else
                    {
                        [resultDic2 setValue:value2 forKey:key2];//Second floor
                    }
                }
                [arr2 addObject:resultDic2];//Second floor
            }
            else if([value1 isKindOfClass:[NSArray class]])
            {
                NSArray *arr3 = (NSArray *)value1;//2 floors
                NSMutableArray *arr4 = [NSMutableArray array];
                for(NSInteger i3 = 0;i3 <arr3.count; i3++)
                {
                    id value5 = arr3[i3];//3rd floor
                    if(nil == value5)
                    {
                        //No, preventive programming
                    }
                    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];//4th floor
                        NSDictionary *dic5 = (NSDictionary *)value5;
                        for(NSInteger i6 = 0;i6 <dic5.allKeys.count; i6++)
                        {
                            NSString *key7 = dic5.allKeys[i6];//5th floor
                            NSString *value7 = dic5.allValues[i6];
                            if(nil == value7)
                            {
                                //No, preventive programming
                            }
                            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];//Second floor
            }//Array dictionary
            else
            {
                [arr2 addObject:value1];//Second floor
            }
        }
        return arr2;//Layer 1 return array - Dictionary - array - Dictionary
    }
    else
    {
        return result;
    }

}

Topics: iOS Algorithm