We Spire
We Spire

Reputation: 27

Get random NSDictionary Key?

Hello StackOverflow Community,

<plist>
<dict>
    <key>Non Random Key</key>
    <dict>
        <key>Random Key</key>
        <dict>
            <key>Hello</key>
            <string>Hey</string>
            <key>Banana</key>
            <string>Bread</string>
        </dict>
    </dict>
</dict>
</plist>

This is my Plist File ... /this/is/the/path.plist

Now i want to get the "Hey" String from "Hello".

Solution for Non Random Key! But i have a random Key, also this issn´t the Solution ...

NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/this/is/the/path.plist"];

NSString *value;
value = [plistDict objectForKey:@"/Non Random Key/Random Key/Hello"];
// value is "Hey"

But how it works with this "Random Key"?

Thank you in Advance.

And maybe, you can explain me, how i set a random key? Also the Example at the Top but instead "Get" i would "Set" :)

Upvotes: 2

Views: 771

Answers (1)

CarlJ
CarlJ

Reputation: 9481

you can iterate over the "Non Random Key" Dictionary. this should help: NSDictionary iterate

NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"/this/is/the/path.plist"];

NSDictionary *nonRandomDic = [plistDic valueForKey:@"NonRandomKey"];


NSEnumerator *enumerator = [nonRandomDic keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
  NSString *tmp = [nonRandomDic objectForKey:key];
  NSLog(@"Your String %@ from the Random Key %@", tmp, key);
}

Upvotes: 1

Related Questions