Sae Us
Sae Us

Reputation: 277

iOS Accessing Dictionary

I have a plist which looks like this. I usually access using the following lines of code:

enter image description here

NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
CGFloat redFloat = (CGFloat)[[plistDict objectForKey:@"red"] floatValue];

However as it is nested - how do I access the colour red in BackgroundColour?

Thanks.

Upvotes: 2

Views: 199

Answers (3)

Krishna Kumar
Krishna Kumar

Reputation: 1652

CGFloat redFloat = [[[plistDict objectForKey:@"BackgroundColour"] objectForKey:@"red"] floatValue];

Upvotes: 0

Wain
Wain

Reputation: 119031

You can use valueForKeyPath: to shortcut too:

CGFloat redFloat = (CGFloat)[[plistDict valueForKeyPath:@"BackgroundColour.red"] floatValue];

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726609

Absolutely: reading plist into a dictionary gives you a dictionary of dictionaries, so you need to access BackgroundColour first:

CGFloat redFloat = (CGFloat)[[[plistDict objectForKey:@"BackgroundColour"] objectForKey:@"red"] floatValue];

New syntax lets you write it with fewer characters:

CGFloat redFloat = (CGFloat)[plistDict[@"BackgroundColour"][@"red"] floatValue];

Upvotes: 3

Related Questions