Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

DennisBlah

macrumors 6502
Original poster
Dec 5, 2013
485
2
The Netherlands
Hi all,

I'm working on a dictionary which is holding all kinds of info that will be used all over in the app.

My question is.. How can I 'add' variables instead of overwriting it all ?

My dictionary is going to hold multiple dictionaries.
How can I add a new dictionary that is already set up with a temporary holder, into my main dictionary

Code:
NSMutableDictionary *temp = [NSMutableDictionary alloc] initWithObjectsAndKeys: nil];
while(....) {
 [temp setObject: @"testy" forKey: @"variable"];
 ...
}
 [mainDictionary setObject: temp forKey: @"main"];

This is overwriting all values inside main. However I just want to add this temp dictionary to the main. How can I do this ?

-----
Temporary solved by workaround.

Looking into core data or sqlite3
 
Last edited:
Sounds to me like you are reusing the key "main". Each 'temp' dictionary needs it's own unique name when adding it to another dictionary, e.g.: main1, main2, etc.
 
Sounds to me like you are reusing the key "main". Each 'temp' dictionary needs it's own unique name when adding it to another dictionary, e.g.: main1, main2, etc.

Indeed. But the thing is.

The app will represent multiple ehmm... lets say companies.
mainDictionary:
company1
company2
company3

Each company will get an own dictionary
mainDictionary:
company1
--Info (Is dictionary as well)
company2
--Info (Is dictionary as well)
company3
--Info (Is dictionary as well)

So far so good. Now I want to add another dictionary to the company. For example gallery
mainDictionary:
company1
--Info (Is a dictionary as well)
--Gallery (Is a dictionary as well)
company2
--Info (Is a dictionary as well)
--Gallery (Is a dictionary as well)
company3
--Info (Is a dictionary as well)
--Gallery (Is a dictionary as well)

This part, is a pain. How do I add this dictionary? In my topic its about temp.

[mainDictionary setObject: temp forKey: @"company1"]
This will cause info to be lost.

At the moment I'm working around it by adding current info dictionary into another temp.
For example:
Code:
 NSMutableDictionary *tempOrg = [mainDictionary objectForKey: @"company1"];
 NSMutableDictionary *temp = [[NSMutableDictionary alloc] initWithObjectsAndKeys: nil];
 while(...) {
  [temp setObject @"test" forKey:"variable"];
 }
 [tempOrg setObject: temp forKey: @"gallery"];
 [mainDictionary setObject: tempOrg forKey: @"company1"];

I was hoping for a nicer solution than this. Because I don't want to rebuild the whole dictionary, but just add this .. 'gallery' into the company1 without losing the info inside company1.
 
The problem is that you are going to have to nest NSDictionaries and it is going to be a big, big mess.

Why not core data? It would be much cleaner, simpler and faster
 
core data ?
I'll have to look into that, I'm kinda new into this iOS coding.
Thanks for the advice!

I also been thinking of sqlite, but that's a lil pain in the ****

Still I think its also a shame that we can't directly connect to a mySQL server, and throw some queries over (without a php api between it)
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.