Reputation: 878
I created a plist and the structure looks like:
Root - NSDictionary
firstName - NSArray
lastName - NSArray
I'm not sure how I access this data after I get the dictionary. When I log the contents of my dictionary, like so
NSDictionary *presetsDict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.presetsDictionary = presetsDict;
for (NSString *key in self.presetsDictionary) {
NSLog(@"key:%@, object:%@", key, [self.presetsDictionary objectForKey:key]);
I get
key:Root, object: {
lastName = (my lastName array, , , , ) // filled with the contents of my plist
firstName - (my firstName array, , , ,)
I was under the assumption that I would be able to get the lastName array and firstName array with something like
NSArray *fArray = [self.presetsDictionary objectForKey:@"firstName"];
But that returns nil. How do I get access to my NSDictionary in the plist? Thanks.
Upvotes: 0
Views: 536
Reputation: 1831
It looks like you need to do something like: NSArray *fArray = [self.presetsDictionary valueForKeyPath:@"Root.firstName"];
In other words, it looks like you have nested dictionaries, and the only entry in the base dictionary is a single Key, Root, with the other data being the associated value.
Upvotes: 0
Reputation: 78353
It looks like your top-level object is a dictionary with one key named Root which contains your core dictionary (where your firstName and lastName keys live). So, to access this, you'd have to go through the object keyed by Root first. You can either get that dictionary and then access its keys, or use a keypath that includes the Root key:
// Accessing through the Root entry
NSDictionary* rootDict = [[self presetsDictionary] objectForKey:@"Root"];
NSArray* firstNames = [rootDict objectForKey:@"firstName"];
NSArray* lastNames = [rootDict objectForKey:@"lastName"];
// Accessing using the keypath with Root
NSArray* firstNames = [[self presetsDictionary] valueForKeyPath:@"Root.firstName"];
NSArray* lastNames = [[self presetsDictionary] valueForKeyPath:@"Root.lastName"];
Upvotes: 4