ArrayWithContentsOfFile cannot read plist file

I have a plist with an array that contains 2 rows, see image:

http://imageshack.us/photo/my-images/263/myplist.png/

and in my viewDidLoad I will add the following:

 NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; NSArray *array = [NSArray arrayWithContentsOfFile:file]; NSLog(@"array = %@", array); 

But I always had null why? Thanks for the help.

UPDATE: the solution is that you need to manually edit the plist file (the default is Xcode4 Dictionnary)

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <array> <string>HO</string> <string>HI</string> </array> </array> </plist> 
+6
source share
5 answers

Most likely, your plist is a dictionary containing an array (Xcode 4 does not allow you to create plists with an array as the root object). Try something like this:

 NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file]; NSArray *array = [dict objectForKey:@"Array"]; NSLog(@"%@", array); 
+13
source

plist should start with root key

in your application you need to change the root key name as it cannot be a data type

0
source

I studied this topic and found some differences in the plist file.

For example, I have a plist source file in sample code that is read using the method
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];

xml source file:

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <dict> <key>nameKey</key> <string>Number One</string> <key>imageKey</key> <string>small_one.png</string> </dict> <dict> <key>nameKey</key> <string>Number Two</string> <key>imageKey</key> <string>small_two.png</string> </dict> </array> </plist> 

When I tried to create the same file above in the Xcode interface using Add a new list of file properties, and after editing the placement of the element, the same path is displayed in the interface for the file above, [[NSArray alloc] initWithContentsOfFile:path]; fails, and I found a problem in the structure of the xml plist file. A file created using the Xcode interface results in an xml plist file:

 <plist version="1.0"> <dict> <key>item 0</key> <dict> <key>imageKey</key> <string>image1.jpg</string> </dict> <key>item 1</key> <dict> <key>imageKey</key> <string>image2.jpg</string> </dict> </dict> </plist> 

I fixed the change directly in the xml file as the same first example, and the method works fine.

Point in the Xcode interface for the plist file, you do not see these differences. Both look the same. Maybe my Xcode 4.1

0
source

The correct answer is: the root element of IS is already an array (in Xcode4 .. not sure about others). Therefore, if you place another array, as shown in its screenshot, you will get an array containing an array with two string values.

0
source

plist this type:

enter image description here

OR (basically both are the same)

enter image description here

It can be read as follows:

 NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"]; _arrFeats = [NSArray arrayWithContentsOfFile:file]; NSLog(@"%i", _arrFeats.count); 

Where appFeatures is the name of plist . (iOS 6.1)

0
source

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


All Articles