How can I access a key nested in a .plist file?

Discussion in 'iOS Development' started by bamhm182, Sep 14, 2010.

  1. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    I'm trying to create an application that gets information from a .plist file, so far I'm pretty happy with how the application is turning out, but I'm having some trouble with the .plist. I've been screwing around in a test file seeing if I can get it figured out without getting distracted by the rest of the code in the actual app. I have this as the .plist.

    Code:
    <dict>
    	<key>Dictionary</key>
    	<dict>
    		<key>readKey</key>
    		<string>This is read from nested dict.</string>
    	</dict>
    	<key>readKey</key>
    	<string>If this displays, it works! (Normal)</string>
    	<key>writeKey</key>
    	<string>[empty]</string>
    </dict>
    </plist>
    
    This is the code I'm using to read from the non-nested "readKey" key.

    Code:
    	NSString *path = [[NSBundle mainBundle] bundlePath];
    	NSString *finalPath = [path stringByAppendingPathComponent:@"testPlist.plist"];
    	NSDictionary* plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
    	NSString *value = [plistDictionary objectForKey:@"readKey"];
    	readLabel.text = value;
    
    I have done just about everything I can think of(yes, including searching) and I am unable to find a solution. I want value to change readLabel.text to "This is read from nested dict." If someone could help me out, that'd be fantastic...
  2. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    So easy.

    You make a new dictionary with the contents of objectForKey

    Please Register or Log in to view images

    ictionary (in this case), then, from that dictionary, you get the objectForKey:readKey, and you'll get the string that says "This is read from nested dict."
  3. bamhm182

    bamhm182 Member

    Joined:
    Mar 2, 2009
    Messages:
    544
    Likes Received:
    0
    Device:
    3G iPod touch
    God, that's so simple! I knew it had to be, but I just wasn't able to figure it out for anything last night. I was going about it the wrong way... :\

    Thank you for your help Pelaez!

    Please Register or Log in to view images

Share This Page