简体   繁体   中英

How do I create array of dictionary in objective c

I am so far new in iOS. I tried a lot to get an output but didn't get. Please help, I want to store data into an Array like this format in Objective-C language:

(
    {
    Appearance = Copper;
    Bitterness = 50;
    Style = "India Pale Ale (IPA)";
},
    {
    Appearance = "Jet Black";
    Bitterness = 35;
    Style = Stout;
},
    {
    Appearance = Copper;
    Bitterness = 25;
    Style = "English Brown Ale";
},
    {
    Appearance = "Deep Gold";
    Bitterness = 25;
    Style = Bock;
}
)

can anyone please help me out.

Here is the way to create Array of Dictionary :

Objective C :

NSArray *arrTemp = @[
                     @{@"Appearance" : @"Copper",
                       @"Bitterness" : @(50),
                       @"Style" : @"India Pale Ale (IPA)"},

                     @{@"Appearance" : @"Jet Black",
                       @"Bitterness" : @(35),
                       @"Style" : @"Stout"},

                     @{@"Appearance" : @"Copper",
                       @"Bitterness" : @(25),
                       @"Style" : @"English Brown Ale"},

                     @{@"Appearance" : @"Deep Gold",
                       @"Bitterness" : @(25),
                       @"Style" : @"Bock"}
                     ];

Swift :

        let arrTemp: [[String:Any]] = [
                                    ["Appearance" : "Copper",
                                     "Bitterness" : 50,
                                     "Style" : "India Pale Ale (IPA)"],

                                    ["Appearance" : "Jet Black",
                                    "Bitterness" : 35,
                                    "Style" : "Stout"],

                                    ["Appearance" : "Copper",
                                    "Bitterness" : 25,
                                    "Style" : "English Brown Ale"],

                                    ["Appearance" : "Deep Gold",
                                    "Bitterness" : 25,
                                    "Style" : "Bock"]
                                  ]

Dictionaries are initialized like this:

NSDictionary *dict = @{ key : value, key2 : value2};

Arrays are initialized like this:

NSArray *array = @[Object1, Object2]

So you can create Dictionaries object as per your data and add to array like

NSArray *array = @[DicObject1, DicObject2]

Any doubt plz comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM