How do I create array of dictionary in objective c
Clash Royale CLAN TAG#URR8PPP
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:
Array
Objective-C
(
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.
Codable
show your tried code
– Anbu.karthik
Aug 6 at 7:54
@QuocNguyen The question is tagged Objective-C
– Larme
Aug 6 at 8:00
@Larme, I didn't see it. In objective-c, you can create a
Class
instead of– Quoc Nguyen
Aug 6 at 8:03
Class
thanku all for your valuable time
– Abhishek Suryawanshi
Aug 6 at 8:39
2 Answers
2
Here is the way to create Array
of Dictionary
:
Array
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"]
]
thnx for you reply @VRAwesome
– Abhishek Suryawanshi
Aug 6 at 8:50
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.
it helps a lot :) thnx @vivekDas
– Abhishek Suryawanshi
Aug 6 at 8:51
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You should create a
Codable
(developer.apple.com/documentation/swift/codable) object instead of dictionay to store your array– Quoc Nguyen
Aug 6 at 7:51