Resolved NSMutableDictionary storing variables

Discussion in 'iOS Programming' started by DennisBlah, Jul 19, 2014.

  1. DennisBlah, Jul 19, 2014
    Last edited: Jul 20, 2014

    DennisBlah macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #1
    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
     
  2. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #2
    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.
     
  3. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #3
    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.
     
  4. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #4
    NSDictionary might not be the best solution here. Why do you want to use them?
     
  5. DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #5
    Because I'm storing this data in a plist, and it will contain arrays and other dictionaries. For settings, styling, buttons, user login hash and stuff.
     
  6. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #6
    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
     
  7. DennisBlah, Jul 19, 2014
    Last edited: Jul 19, 2014

    DennisBlah thread starter macrumors 6502

    DennisBlah

    Joined:
    Dec 5, 2013
    Location:
    The Netherlands
    #7
    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)
     
  8. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #8
    Just make some model classes or even C structs.
     

Share This Page