Convert the data in the dictionary in the array to a URL in objective-C.

Source: Internet
Author: User

Maybe the title above is a bit verbal. Everyone who has learned PHP knows that the subscripts of arrays in PHP allow us to customize them. Arrays in PHP are exactly key-value pairs. In OC, we need to use a dictionary (Dictionary) to store, of course, Java uses Map to store key-value pairs. Below we will implement a very practical small example, the network request has get and post methods, both methods have their advantages. The Post method is used more when the form is submitted, and the Get method is used to pass the URL. When programming in PHP, you will often stitch URLs to pass parameters or requests. In IOS development, you will request some data from the server through the URL, so the stitching of the URL is indispensable. The following is not a deep technology, just a collection in OC A small application module of this class is welcome to criticize and correct, please indicate the source when reprinted.

        In OC, how do we stitch the data in the dictionary into the URL string we want to request? Here is a requirement: there are multiple dictionaries in an array, and the data in each dictionary is a parameter in a URL. All we need to do is to convert each dictionary to a URL, and put each URL in an array return. The following code applies to arrays, strings, and dictionaries.

        The above requirements should be a piece of cake for those who are proficient in operating dictionaries, arrays, and strings. I wrote it today to record a little bit of learning. The URLs are spliced at the top. Dictionary, stay tuned! Without further ado, the code is still up.

        1. Generate test data

            The keys in the dictionary are generally initialized through macro definitions, in order to facilitate maintenance and improve the efficiency of code writing. Here is the macro definition for key:


// define dictionary keys
#define A @ "a"
#define B @ "b"
#define C @ "c"
#define D @ "d"
    ??? Add test data


// Accommodate the string after string concatenation
NSMutableArray * arrayURL = [NSMutableArray new];
 
// Array of variable dictionaries
NSMutableArray * arrayDic = [NSMutableArray new];
 
 
// Generate test data
NSDictionary * dic1 = @ (A: @ 10,
                       B: @ 20};
 
 
NSDictionary * dic2 = @ (A: @ 10,
                       B: @ 20,
                       C: @ 30};
 
 
NSDictionary * dic3 = @ (A: @ 10,
                       C: @ 30};
// Add array to dictionary
[arrayDic addObject: dic1];
[arrayDic addObject: dic2];
[arrayDic addObject: dic3];
     2. The next thing we need to do is to convert the data in the dictionary in the variable array above into a URL. The specific process of conversion is as follows


// Retrieve the dictionary from the array, and then stitch each into a url
for (int i = 0; i <arrayDic.count; i ++)
{
     
    NSLog (@ "operation on the% d dictionary in the array \ n:% @", i + 1, arrayDic [i]);
     
    // URL
    NSMutableString * URL = [NSMutableString stringWithFormat: @ "http://www.baidu.com"];
    // Get all the keys of the dictionary
    NSArray * keys = [arrayDic [i] allKeys];
     
    // Splice strings
    for (int j = 0; j <keys.count; j ++)
    {
        NSString * string;
        if (j == 0)
        {
            // Add when splicing?
            string = [NSString stringWithFormat: @ "?% @ =% @", keys [j], arrayDic [i] [keys [j]]];
             
        }
        else
        {
            // Add & when stitching
            string = [NSString stringWithFormat: @ "&% @ =% @", keys [j], arrayDic [i] [keys [j]]];
        }
        // Splice strings
        [URL appendString: string];
         
    }
    NSLog (@ "URL of the% d dictionary conversion:% @", i + 1, URL);
    // Join the array
    [arrayURL addObject: URL];
     
}
    ??? Code description:

    ??? 1. Iterate over each dictionary in the array

    ??? 2. Traversing the key-value pairs in each dictionary

    ??? 3. If the first parameter is added in the url?

    4. If not the first parameter, add &

    ??? 5. Add the spliced string URL to the variable array and return the array containing the URL

    ??? The final conversion result is:


2014-08-12 11: 41: 18.927 HelloOC [997: 303] Conversion completed:
(
    "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"
)
    ?? Then we should reverse the converted knot into the original data format? The corresponding method will be mentioned in the following blog. Thank you friends for your support. We also want to criticize and correct the shortcomings.

Objective-C convert data in dictionary in array into URL, bubuko.com

Objective-C convert the data in the dictionary in the array into a URL

Tags: style http color java os io data for

Original address: http://www.cnblogs.com/ludashi/p/3906964.html

Related Article

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.