Nested Dictionaries

Discussion in 'Mac Programming' started by GerardGrundy, Sep 8, 2013.

  1. GerardGrundy macrumors newbie

    Joined:
    Sep 8, 2013
    #1
    Hi,

    I'm a total beginner so please excuse if I totally phrase this wrong but I'm using 2 dictionaries. One is a main dictionary for storing the other dictionary that has 3 keys and objects. I'm trying to assign one of the objects of the inner dictionary to a a new key in the dictionary. though not sure how to call this dictionary and get the object. Here's my code:

    NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
    NSMutableDictionary *innerDictionary = [NSMutableDictionary dictionary];

    NSNumber *value = [NSNumber numberWithInt:1]
    [innerDictionary setObject:value forKey:mad:"key1"];
    [innerDictionary setObject:value forKey:mad:"other2"];
    [innerDictionary setObject:value forKey:mad:"andThis3"];

    [dictionary setObject:spinCompareDict forKey:value];

    This is called when the function is called.
    On the second call Im trying to grab the @"key1" and set it to @"andThis3" key though they are both nested in a disctionary with the key value which is set to 1. How could I do this?
    Thank you if you can help.
     
  2. devilofspades, Sep 24, 2013
    Last edited: Sep 24, 2013

    devilofspades macrumors member

    Joined:
    Jul 20, 2011
    #2
    right off the bat i can say your example doesn't quite match your question. your "spinCompareDict" is the only dictionaty nested in your "dictionary" dictionary, and you don't show any of its info so it will be hard to reference your example in the explination but i get what you are saying.

    when dealing with nested dictionaries the best way is to you "kvc" or "key value coding". if you use something like "key.nestedkey", that would be how you access the nested dictionary in the first dictionary. i will be abriviating some of the syntax but im sure you will pickup what im getting at.

    say for example...

    NSMutableDict *nameDict1 = [[NSMutableDictionary alloc]init];
    [nameDict1 setObject:someObject1 forKey:key1];
    [nameDict1 setObject:someObject2 forKey:key2];
    [nameDict1 setObject:someObject3 forKey:key3];

    NSMutableDict *nameDict2 = [[NSMutableDictionary alloc]init];
    [nameDict2 setObject:someOtherObject1 forKey:key1];
    [nameDict2 setObject:someOtherObject2 forKey:key2];
    [nameDict2 setObject:someOtherObject3 forKey:key3];

    then you want to nest those...

    NSMutableDict *masterDict = [NSMutableDict...
    [masterDict setObject:nameDict1 forKey:dict1];
    [masterDict setObject:nameDict2 forKey:dict2];

    so that would be two dictionaries nested in another dictionary. using the key value is how you get the data. so you would need to use the masterDict "valueForKeyPath:" method. so if i wanted the "someObject2" from the "nameDict1" i would do something like

    [masterDict valueForKeyPath:mad:"dict1.key2"];

    this would get the "dict1" key from the "masterDict" (which is the nameDict1), and then get the nested value for "key2" from key "dict1", which is "someObject2". hope that helps. if you are just starting out, read as much as you can about "kvc" & "kvo". they are one for the key (no pun intended) features of objective-c. they beauty of it and the way it works is by using the string value for the value and/or key. you can even set values this way using "setValue:forKey:". this becomes very powerful when passing in variables and using the NSString method "stringWithFormat:".
     

Share This Page