• URL 转化为 Dictionary 字典


    通过网上查找,参考了别人

    url  为:

    woaiwodejiaxiang://cn.jiaxiang.ZG/loader?dt=1254896584562&cityId=tr&deviceNumber=13185058252&userId=20985532&roleType=2&state=1&token=dad8fa595beda8306ea1aa01250aeb0&deviceIMEI=3951545154614522

    1.

    NSString *urlString = [NSString stringWithFormat:@"%@",url];// 将URL转化为NSString

    NSMutableArray *urlArray =[NSMutableArray arrayWithObject:urlString];// 并放在数组中

    NSRange range = [urlArray[0] rangeOfString:@"?"]; // 获取问号的位置 问号后面是参数列表。   urlArray[0] :我只有一个元素在数组中

    NSString *propertys = [urlArray[0] substringFromIndex:(int)range.location]; 参数列表

    NSArray *subArray =[propertys componentsSeparatedByString:@"&"]; // 进行字符串的拆分,通过&来拆分,

    2.

    NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithCapacity:8];// 8 是 我的URL中有8个参数 最总得到的字典有8个键值对

    for (int j = 0; j<subArray.count; j++) {

            NSArray *dicArray = [subArray[j] componentsSeparatedByString:@"="]; // 等号“=” 是用来分割键值对

            [tempDic setObject:dicArray[1] forKey:dicArray[0]];// 给字典添加键值对关系,(我参考别人的)

        }

        NSLog(@"打印参数列表生成的字典: %@", tempDic);// 输出转化的字典 

    {
    "?dt" = 1254896584562;
    cityId = gy;
    deviceIMEI = 3951545154614522;
    deviceNumber = 13185058252;
    roleType = 1;
    state = 1;
    token = dad8fa595beda8306ea1aa01250aeb0;
    userId = 20985532;
    }

  • 相关阅读:
    面试题 16:反转链表
    Makefile学习之路6————通过函数增强功能
    Makefile学习之路5————变量
    Makefile学习之路3————规则的运行
    LED灯C语言的点亮方式
    LED灯汇编机器码的点亮方式
    Linux的进阶命令
    入门命令2
    shell命令解析器功能说明及入门命令1
    C++基础 — C++中的变量和三目运算符
  • 原文地址:https://www.cnblogs.com/tom2015010203/p/4788977.html
Copyright © 2020-2023  润新知