美文网首页
iOS字符串数组转NSDictionary操作笔记

iOS字符串数组转NSDictionary操作笔记

作者: 数字d | 来源:发表于2021-09-09 17:38 被阅读0次

    使用场景:
    app分享之后,其他用户点击分享链接,进行app下载,在下载之后打开app之后会将分享链接中的一些参数带进app,app根据这些参数来做一些业务处理.
    这里是获取到的数据格式[appdata paramsData]的示例,实际的类型是字符串

    userid=info&username=valueName&useravatar=avatar
    

    为了方便使用和解析,开发者希望拿到的数据格式应该为

    {
          "userid":"info",
          "username":"valueName",
          "useravatar":"avatar"
    }
    

    为了获取如上所示的数据类型,需要做一些操作

    方案一 :

    第一步: 将字符串中的"&"符号替换成"=",并根据"="对字符串进行分割,分割结果为字符串数组

        NSString * comString = [string stringByReplacingOccurrencesOfString:@"&" withString:@"="];
        NSArray * array = [comString componentsSeparatedByString:@"="];
    

    第二步:数组转字典

        NSMutableDictionary * dicInfo = [NSMutableDictionary dictionaryWithCapacity:0];
        for (int i = 0; i < array.count - 1; i += 2) {
            NSString * stringKey = array[i];
            NSString * stringValue = array[i + 1];
            [dicInfo setValue:stringValue forKey:stringKey];
        }
    
    

    方案一所有的实现代码:

    -(NSDictionary *)dicWithAndEqualString:(NSString *)string {
        
        NSString * comString = [string stringByReplacingOccurrencesOfString:@"&" withString:@"="];
        NSLog(@"替换字&符串之后的str=%@",comString);
        NSArray * array = [comString componentsSeparatedByString:@"="];
        NSLog(@"分割之后的arr=%@",array);
        NSMutableDictionary * dicInfo = [NSMutableDictionary dictionaryWithCapacity:0];
        for (int i = 0; i < array.count - 1; i += 2) {
            NSString * stringKey = array[i];
            NSString * stringValue = array[i + 1];
            [dicInfo setValue:stringValue forKey:stringKey];
        }
        NSLog(@"得到的结果字典=%@",dicInfo);
        return dicInfo;
    }
    

    方案一打印结果:

    2021-09-09 17:45:47.782308+0800 raceme[2641:1860334] 初始的字符串str = userid=info&username=valueName&useravatar=avatar
    2021-09-09 17:45:47.782402+0800 raceme[2641:1860334] 替换字&符串之后的str=userid=info=username=valueName=useravatar=avatar
    2021-09-09 17:45:47.782485+0800 raceme[2641:1860334] 分割之后的arr=(
        userid,
        info,
        username,
        valueName,
        useravatar,
        avatar
    )
    2021-09-09 17:45:47.782555+0800 raceme[2641:1860334] 得到的结果字典={
        useravatar = avatar;
        userid = info;
        username = valueName;
    }
    
    

    方案二:

    第一步:将字符串根据"="和"&"分割成数组,这里因为使用componentsSeparatedByCharactersInSet方法分割字符串会出现空字符串@"",因此需要对得到的数组字符串中包含的空值@""进行去除操作,防止下一步的key和value值因为空值导致赋值错乱

        NSMutableArray * array = [NSMutableArray arrayWithArray:[string componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"=&"]]];
        [array removeObject:@""];
    

    第二步:字典转数数组

        NSMutableDictionary * dicInfo = [NSMutableDictionary dictionaryWithCapacity:0];
        for (int i = 0; i < array.count - 1; i += 2) {
            NSString * stringKey = array[i];
            NSString * stringValue = array[i + 1];
            [dicInfo setValue:stringValue forKey:stringKey];
        }
    
    

    方案二所有的实现代码:

    -(NSDictionary *)dicWithAndEqualAnotherString:(NSString *)string {
        
        NSMutableArray * array = [NSMutableArray arrayWithArray:[string componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"=&"]]];
        NSLog(@"根据=&字符分割之后的arr=%@",array);
        [array removeObject:@""];
        NSLog(@"去掉空字符串之后arr=%@",array);
    
        NSMutableDictionary * dicInfo = [NSMutableDictionary dictionaryWithCapacity:0];
        for (int i = 0; i < array.count - 1; i += 2) {
            NSString * stringKey = array[i];
            NSString * stringValue = array[i + 1];
            [dicInfo setValue:stringValue forKey:stringKey];
        }
        NSLog(@"得到到的结果字典=%@",dicInfo);
    
        return dicInfo;
    }
    

    方案二的代码打印结果

    2021-09-09 17:44:29.774279+0800 raceme[2639:1859818] 初始的字符串str = userid=info&username=valueName&useravatar=avatar
    2021-09-09 17:44:29.774424+0800 raceme[2639:1859818] 根据=&字符分割之后的arr=(
        userid,
        info,
        username,
        valueName,
        useravatar,
        avatar
    )
    2021-09-09 17:44:29.774481+0800 raceme[2639:1859818] 去掉空字符串之后arr=(
        userid,
        info,
        username,
        valueName,
        useravatar,
        avatar
    )
    2021-09-09 17:44:29.774551+0800 raceme[2639:1859818] 得到到的结果字典={
        useravatar = avatar;
        userid = info;
        username = valueName;
    }
    
    

    相关文章

      网友评论

          本文标题:iOS字符串数组转NSDictionary操作笔记

          本文链接:https://www.haomeiwen.com/subject/ydfwwltx.html