开发者

arrayWithContentsOfFile can't read plist file

i have a plist with an array that contain 2 strings see image :

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

and in my viewDidLoad i add this :

NSString *file = [[NSBundle  mainBundle] pathForResource:@"Data"    ofType:@"plist"];

NSArray *array = [NSArr开发者_运维知识库ay    arrayWithContentsOfFile:file];

NSLog(@"array = %@", array);

But i got always null why? Thanks for help.

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

<?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>


Most likely, your plist is a dictionary that contains an array (Xcode 4 doesn't allow 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);


arrayWithContentsOfFile can't read plist file

in you application you need to change the root key name as it can not be datatype


I've been studied this topic and discover some differences in plist file.

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

the xml original file is :

<?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 this same file above in Xcode Interface, using Add new File Property List, and after editing puting the item the same way is show in interface for the file above, the method [[NSArray alloc] initWithContentsOfFile:path]; fail, and I found the issue at structure of xml plist file. The file created using XCode Interface, result in 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 change direct in xml file as same the first example, and the method works fine.

The point is in XCode interface for plist file, you cant see these differences. The both look the same. Maybe is my Xcode 4.1


The correct answer is, the root item IS already an array (in XCode4.. not sure about the others). So if you put another array as neil d showed in his screen shot, you will end up with an array that contains an array with the two string values.


plist of this type:

arrayWithContentsOfFile can't read plist file

OR (basically both are same)

arrayWithContentsOfFile can't read plist file

Can be read like this:

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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜