Jon
Jon

Reputation: 4732

Loading Array From .Plist

I'm trying to load my array from an array in my .Plist but its not working.

The plist looks like this:

enter image description here

This is the code I'm using:

NSString *path = [[NSBundle mainBundle]pathForResource:@"DiseasePropertyList" ofType:@"plist"];
NSMutableArray *rootLevel = [[NSMutableArray alloc]initWithContentsOfFile:path];
self.myArray = rootLevel;
[rootLevel release];

Upvotes: 6

Views: 8747

Answers (5)

manishsharma93
manishsharma93

Reputation: 1049

Use this method it worked fine for me.

NSArray *country= [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CountryList" ofType:@"plist"]];

Upvotes: 0

Erwan
Erwan

Reputation: 3729

If your plist file is in your application bundle, the easiest is this:

NSArray *myArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"filename_without_extension" ofType:@"plist"]];

Upvotes: 2

Andrew Toth
Andrew Toth

Reputation: 419

Your plist is actually a dictionary. The array is the object of that dictionary for key "Array". You can make the plist into an array in Xcode 4 by selecting "Array" and cutting (CMD-x), and then pasting into the empty plist (CMD v).

Upvotes: 5

ChangUZ
ChangUZ

Reputation: 5440

Try this. Please change file name. It works fine.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *recentFilePath = [documentsDirectory stringByAppendingPathComponent:@"recent.plist"];
NSArray *history = [NSArray arrayWithContentsOfFile:recentFilePath];

Upvotes: 9

Michael Bai
Michael Bai

Reputation: 586

NSString *pathStr = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [settingsBundlePath stringByAppendingPathComponent:@"yourproject.plist"]; 
NSDictionary *settingsDict = [NSDictionary dictionaryWithContentsOfFile:finalPath];

and just check the description of settingsDict. Hope this help you.

Upvotes: 2

Related Questions