user916316
user916316

Reputation: 69

plists - iOS development (NSString)

I've created a plist called "hospitals", inserted first row as an array with three items as strings.

what's wrong with my code:

NSArray *hospitals = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Hospitals" ofType:@"plist"]];
    NSString *first = [[NSString alloc] initWithFormat:@"%@", [hospitals objectAtIndex:0]];
    NSLog(@"%@",first);

This is giving me (null) as a result!

Upvotes: 0

Views: 266

Answers (2)

Nekto
Nekto

Reputation: 17877

Try this:

NSString *errorDesc = nil;
NSPropertyListFormat format;
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:[[NSBundle mainBundle] pathForResource:@"Hospitals" ofType:@"plist"]];
NSMutableDictionary *properties = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
NSArray *hospitals = (NSArray *)[properties valueForKey:@"names"]; 
NSString *first = [hospitals objectAtIndex:0];
NSLog(@"%@",first);

Upvotes: 0

user142019
user142019

Reputation:

Either the property list file doesn't exist (check the "Copy Resources" build phase of your target), has a different name (note that iOS' file system is case sensitive), isn't a property list at all (maybe it has a syntax error), isn't readable or the property list's root isn't an array, but a dictionary.

If hospitals == nil either of the above is the problem.

Upvotes: 1

Related Questions