Convert URL request parameters to dictionaries in objective-c

Source: Internet
Author: User
Tags string splitter

The last blog is to convert the URL into a dictionary, then how do we encapsulate the parameters in the URL request into a dictionary, and then encapsulate the array? Working with strings in OC skilled small partners should feel that this is a a+b problem, yes, the parameters in the URL is converted to a dictionary is mainly the interception of the string, the key is how to cut a string, the argument list can be converted to a key value pair. The following is a side dish of their own conversion ideas, if there is a better way to end the method also please criticize, learn to communicate with each other, reproduced please indicate the source.

First we'll have a string splitter function componentsseparatedbystring:@ "&", splitting the string in & and then returning the string array

? The format of the input data is as follows (an array of strings with URLs):

12345 (    "http://www.baidu.com?a=10&b=20",    "http://www.baidu.com?a=10&b=20&c=30",    "http://www.baidu.com?a=10&c=30")

? ? The next step is to convert the argument list in each URL into a dictionary, the main code is as follows:

?    ? ? Code Description:

?    ?    ? 1. Get the parameter list from the starting position of the parameter list in the URL and get it by means of the method rangeofstring method.

?    ?    ? ? 2. We can extract the parameter list from the URL string and get it through the substringfromindex after we get to the location.

?    ?    ? ? 3. Use Componentsseparatedbystring to split the parameter list by each item

?    ?    ? 4. Splitting each key pair by componentsseparatedbystring

?    ?    ? 5. Storing key-value pairs in a dictionary

?    ?    ? ? 5. Storing dictionaries in arrays

123456789101112131415161718192021222324252627282930313233343536373839404142434445 NSMutableArray * dataArray(){    //用来作为函数的返回值,数组里里面可以存放每个url转换的字典    NSMutableArray *arrayData = [NSMutableArray arrayWithCapacity:4];        //获取数组,数组里装得是url    NSMutableArray *arrayURL = appendURL();    NSLog(@"获取到得URL数组如下:\n%@", arrayURL);        //循环对数组中的每个url进行处理,把参数转换为字典    for(inti = 0; i < arrayURL.count; i ++)    {        NSLog(@"第%d个URL的处理过程:%@", i+1, arrayURL[i]);                //获取问号的位置,问号后是参数列表        NSRange range = [arrayURL[i] rangeOfString:@"?"];        NSLog(@"参数列表开始的位置:%d", (int)range.location);                //获取参数列表        NSString *propertys = [arrayURL[i] substringFromIndex:(int)(range.location+1)];        NSLog(@"截取的参数列表:%@", propertys);                //进行字符串的拆分,通过&来拆分,把每个参数分开        NSArray *subArray = [propertys componentsSeparatedByString:@"&"];        NSLog(@"把每个参数列表进行拆分,返回为数组:\n%@", subArray);                //把subArray转换为字典        //tempDic中存放一个URL中转换的键值对        NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithCapacity:4];                for(int j = 0 ; j < subArray.count; j++)        {            //在通过=拆分键和值            NSArray *dicArray = [subArray[j] componentsSeparatedByString:@"="];            NSLog(@"再把每个参数通过=号进行拆分:\n%@", dicArray);            //给字典加入元素            [tempDic setObject:dicArray[1] forKey:dicArray[0]];        }        NSLog(@"打印参数列表生成的字典:\n%@", tempDic);        [arrayData addObject:tempDic];    }    NSLog(@"打印参数字典生成的数组:\n%@", arrayData);        returnarrayData;}

? ? The above code specific steps to perform:

1234567891011121314151617181920212223 2014-08-12 13:46:14.126 HelloOC[1195:303] 第1个URL的处理过程:http://www.baidu.com?a=10&b=202014-08-12 13:46:14.126 HelloOC[1195:303] 参数列表开始的位置:202014-08-12 13:46:14.126 HelloOC[1195:303] 截取的参数列表:a=10&b=202014-08-12 13:46:14.127 HelloOC[1195:303] 把每个参数列表进行拆分,返回为数组:(    "a=10",    "b=20")2014-08-12 13:46:14.127 HelloOC[1195:303] 再把每个参数通过=号进行拆分:(    a,    10)2014-08-12 13:46:14.127 HelloOC[1195:303] 再把每个参数通过=号进行拆分:(    b,    20)2014-08-12 13:46:14.128 HelloOC[1195:303] 打印参数列表生成的字典:{    a = 10;    b = 20;}

Convert URL request parameters to dictionaries in objective-c

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.