Array does not load data from plist

I am not sure what my problem is, since I used this method for a while and have never encountered this problem.

In principle, as the name says. This code loads the data from the plist located in the Resources group.

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

Displayed (null). Does anyone know why?

I have one more setting in the same ViewController and it works fine. However, this particular layer does not load. And I even deleted the plist and just created a duplicate from another to make sure this is not a structure issue.

Am I doing it wrong? Should I use FileManager?

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

OK, I can't believe what I'm going to say. lol

I am using Xcode 4, the way plists are edited in Xcode is different from Xcode 3.x. As in earlier versions, you can select the plists root that you want you to want (e.g. Array, Dictionary, etc.). My problem was that the plist that I was trying to load into NSArray was actually structured like a Dictionary. However, I have never seen this in Xcode, and I don’t know how to see it other than viewing the contents using TextEdit or another text editor.

Pretty stupid, I have to say. Does anyone know why Xcode 4 will not show the plist root, like Xcode 3.x?

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

+6
source share
3 answers

Use a dictionary instead. I had undefined behavior with NSArrays and property lists. eg.

 NSString *myFile = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"plist"]; NSMutableDictionary* myDict = [[NSMutableDictionary alloc]initWithContentsOfFile:myFile]; NSLog(@"%@", myDict); 
+3
source

I had a similar situation. Decision:

  • select * .plist in the tree
  • call context menu
  • select "Open with an external editor"
  • the root will be visible - change "Class" to "Array"
  • save
+1
source

What you have looks right. Make sure the file is in the correct directory and has the contents. You may need NSLog myFile while you are on it.

0
source

Source: https://habr.com/ru/post/885591/


All Articles