Array does not load data from plist
Asked Answered
S

3

6

I'm not sure what exactly is my problem since I have been using this method for a while now and never had this problem.

Basically, as the title says. This code loads data from a plist located in the "Resources" group

NSString *myFile = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"plist"];
myArray = [[NSArray alloc]initWithContentsOfFile:myFile];
NSLog(@"%@", myArray);

It displays (null). Anyone know why?

I have in the same ViewController another one setup and works just fine. This particular plist does not load however. And I've also even deleted the plist and just created a duplicate from the other one to make sure is not a structure issue.

I am doing this the wrong way? Should I use FileManager?

UPDATE -------------------------------------------------------------------------

Ok so, I can't believe what I am about to say. lol

I am using XCode 4, the way you edit plists in Xcode is different from Xcode 3.x. As in the earlier versions you get to choose the root of the plists to be w/e you want them to (Array, Dictionary, etc) My problem was that the plist I was trying to load into a NSArray was in fact structured as a dictionary. However I never saw this in Xcode, and I have no idea how to see this other than browsing the contents using TextEdit or some other text editor.

Pretty silly I must say. Does anyone know why XCode 4 won't show the root of the plist the way XCode 3.x does?

XCode 3.x: http://dl.dropbox.com/u/919254/XCode3.png

XCode 4: http://dl.dropbox.com/u/919254/Screen%20shot%202011-04-10%20at%202.30.35%20AM.png

Sverre answered 10/4, 2011 at 3:16 Comment(0)
A
3

Use a dictionary instead. I've had undefined behavior with NSArrays and property lists. e.g.

NSString *myFile = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"plist"];
NSMutableDictionary* myDict = [[NSMutableDictionary alloc]initWithContentsOfFile:myFile];
NSLog(@"%@", myDict);
Abducent answered 10/4, 2011 at 8:57 Comment(3)
This works... although I do not understand why. The structure of the plist is the exact same as the one I'm using for the rest of my files. :(Sverre
You can't use an NSDictionary or NSMutableDictionaryto load a plist if the top-level object in the plist is an array, and vice versa. This isn't a matter of user preference. Apparently, the OPs problem was that the top-level object in the underlying plist was a dictionary rather than an array. If that's the case, then @ManuelDiaz posted the wrong example plist file.Canikin
Thanks for the clarification. I was having this undefined behavior with Xcode 4 as well. Didn't notice until @Manuel Diaz's update that Xcode 4 hides the plist root.Abducent
N
1

I had a similar situation. Solving:

  1. select *.plist in tree
  2. call context menu
  3. select "Open with external editor"
  4. root will be visible - change "Class" to "Array"
  5. save
Nash answered 30/4, 2011 at 20:37 Comment(0)
B
0

What you have looks right. Check that the file is in the right directory, and has contents. Might want to NSLog myFile while your at it.

Balefire answered 10/4, 2011 at 4:17 Comment(3)
Yeah the file is in the right directory. Here's my log dl.dropbox.com/u/919254/… and the contents dl.dropbox.com/u/919254/…Sverre
All fine, just check the root of the plist file you posted is a Dictionary (looks like an Array). And you're using the right name in you program.Balefire
This is so weird, somehow when I load the plist into a dictionary it gets the values no problem. However the structure is the exact same as the rest of my plists and they load as NSArrays!Sverre

© 2022 - 2024 — McMap. All rights reserved.