zoukankan      html  css  js  c++  java
  • iOS 网络请求中的空类型字符串转换

    创建一个工具类,   .h:

    #import <Foundation/Foundation.h>
    
    @interface MySetNullWithStrTool : NSObject
    
    +(id)changeType:(id)myObj;
    
    @end

    .m:

    #import "MySetNullWithStrTool.h"
    
    @implementation MySetNullWithStrTool
    
    //将NSDictionary中的Null类型的项目转化成@""
    +(NSDictionary *)nullDic:(NSDictionary *)myDic
    {
        NSArray *keyArr = [myDic allKeys];
        NSMutableDictionary *resDic = [[NSMutableDictionary alloc]init];
        for (int i = 0; i < keyArr.count; i ++)
        {
            id obj = [myDic objectForKey:keyArr[i]];
            
            obj = [self changeType:obj];
            
            [resDic setObject:obj forKey:keyArr[i]];
        }
        return resDic;
    }
    
    //将NSArray中的Null类型的项目转化成@""
    +(NSArray *)nullArr:(NSArray *)myArr
    {
        NSMutableArray *resArr = [[NSMutableArray alloc] init];
        for (int i = 0; i < myArr.count; i ++)
        {
            id obj = myArr[i];
            
            obj = [self changeType:obj];
            
            [resArr addObject:obj];
        }
        return resArr;
    }
    
    //将NSString类型的原路返回
    +(NSString *)stringToString:(NSString *)string
    {
        return string;
    }
    
    //将Null类型的项目转化成@""
    +(NSString *)nullToString
    {
        return @"";
    }
    
    //主要方法
    //类型识别:将所有的NSNull类型转化成@""
    +(id)changeType:(id)myObj
    {
        if ([myObj isKindOfClass:[NSDictionary class]])
        {
            return [self nullDic:myObj];
        }
        else if([myObj isKindOfClass:[NSArray class]])
        {
            return [self nullArr:myObj];
        }
        else if([myObj isKindOfClass:[NSString class]])
        {
            return [self stringToString:myObj];
        }
        else if([myObj isKindOfClass:[NSNull class]])
        {
            return [self nullToString];
        }
        else
        {
            return myObj;
        }
    }
    
    
    @end

    调用:

        NSDictionary *dic = (NSDictionary*)responseObject;
        NSDictionary *newDic = [MySetNullWithStrTool changeType:dic];
  • 相关阅读:
    Lambda表达式的演变
    反射小例
    进程外Session
    页面缓存的几种方式
    数据缓存的几种方式
    Session
    Cookie
    AJAX学习
    验证码的实现
    ASP.NET动态显示数据的两种方式
  • 原文地址:https://www.cnblogs.com/dianming/p/7211643.html
Copyright © 2011-2022 走看看