Question

I'm having an issue with reading from a plist I can't seem to figure.

My plist list looks like this (note i've simplified it for examples sake):

enter image description here

Then I'm reading code like this:

NSString *path = [[NSBundle mainBundle] pathForResource:@"ContactDetails" ofType:@"plist"];
NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSArray* allmyKeys = [myDictionary allKeysForObject:@"Name1"];

Any ideas - the issue i keep facing is allmyKeys shows up as containing 0 objects. On debugging myDictionary is correctly populated so not sure why it doesn't work.

Many Thanks

Was it helpful?

Solution

The problem is with this line:

NSArray* allmyKeys = [myDictionary allKeysForObject:@"Name1"]; 

@"Name1" is a key in your dictionary, not a value. Even if it were a value, the string @"Name1" is a different object from the string with the same value in myDictionary, so this call would not do what you expect.

You probably want to access the dictionary by doing

NSDictionary *userDetails = myDictionary[@"Name1"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top