user843337
user843337

Reputation:

Unable to load plist into NSArray

I'm trying to load the following plist into an NSArray:

enter image description here

Here is the code I'm using to load the data:

// Load the data from the plist file
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"MyData" ofType:@"plist"];
NSArray *myArray = [[NSArray alloc] initWithContentsOfFile:filepath];

However when I place a breakpoint after the data has been loaded I notice that the array is empty. Please can someone help me understand what's going wrong?

Upvotes: 1

Views: 1233

Answers (3)

RAUL QUISPE
RAUL QUISPE

Reputation: 799

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"name.plist"];
NSArray *plistData = [NSArray arrayWithContentsOfFile:finalPath];

Upvotes: 1

syclonefx
syclonefx

Reputation: 3000

Your plist file has a dictionary that contains an array. Try this

NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSArray *array = [[NSArray alloc]initWithArray:[dict objectForKey:@"Root"]];

Upvotes: 2

Ignacio Inglese
Ignacio Inglese

Reputation: 2605

Could you try loading it into a NSDictionary and asking for the [ objectForKey:@"Root"] inside it ?

As far as I remember, property lists are key / value maps and your array is just one of those values.

Upvotes: 5

Related Questions