Loading Array From .Plist
Asked Answered
D

5

6

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];
Donata answered 8/8, 2011 at 1:31 Comment(1)
You look dangerous with that list!Locoism
C
9

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];
Calorie answered 8/8, 2011 at 1:52 Comment(1)
Another way: ` NSURL* url = [[NSBundle mainBundle] URLForResource: @"DiseasePropertyList" withExtension: @"plist"]; NSArray* myArray = [NSArray arrayWithContentsOfURL: url];`Neckband
N
5

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).

Nelidanelie answered 8/8, 2011 at 2:33 Comment(2)
I would say this is most likely the issue. You can also view the plist as source and just remove the <dict> and </dict> tags from the file (I do it all the time).Brame
Also remove the <key> .. </key> tag after the starting tag <dict>.. this will definitely solves problem for me....Noreen
G
2
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.

Guiana answered 8/8, 2011 at 1:49 Comment(0)
U
2

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"]];
Ubiquitous answered 2/4, 2014 at 4:39 Comment(0)
H
0

Use this method it worked fine for me.

NSArray *country= [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"CountryList" ofType:@"plist"]];
Hackle answered 7/9, 2015 at 7:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.