Array does not load data from plist
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
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);
I had a similar situation. Solving:
- select *.plist in tree
- call context menu
- select "Open with external editor"
- root will be visible - change "Class" to "Array"
- save
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.
精彩评论