Reputation: 51
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.
Upvotes: 0
Views: 5111
Reputation: 4803
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"]
]
Upvotes: 4
Reputation: 1288
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.
Upvotes: -1