简体   繁体   English

arrayWithContentsOfFile 无法读取 plist 文件

[英]arrayWithContentsOfFile can't read plist file

i have a plist with an array that contain 2 strings see image:我有一个包含 2 个字符串的数组的 plist 参见图片:

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

and in my viewDidLoad i add this:在我的viewDidLoad中我添加了这个:

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

NSArray *array = [NSArray    arrayWithContentsOfFile:file];

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

But i got always null why?但我总是得到null为什么? Thanks for help.感谢帮助。

UPDATE: the solution is that you need to manually edit the plist file (Xcode4 use Dictionnary by default) UPDATE:解决方法是需要手动编辑plist文件(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>

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).最有可能的是,您的 plist 是一个包含数组的字典(Xcode 4 不允许创建以数组作为根对象的 plist)。 Try something like this:尝试这样的事情:

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

plist of this type:这种类型的plist

在此处输入图像描述

OR (basically both are same)或(基本上两者都相同)

在此处输入图像描述

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 .其中appFeaturesplist的名称。 (iOS 6.1) (iOS 6.1)

plist 应该以 Root Key 开头

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.我一直在研究这个主题并发现 plist 文件中的一些差异。

For example, I have the original plist file in sample code that is read with the method例如,我在使用该方法读取的示例代码中有原始 plist 文件
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];

the xml original file is: xml 原始文件为:

<?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];当我尝试在 Xcode 界面中创建相同的文件时,使用添加新文件属性列表,并在编辑后将项目以相同的方式显示在上述文件的界面中,方法[[NSArray alloc] initWithContentsOfFile:path]; fail, and I found the issue at structure of xml plist file.失败,我在 xml plist 文件的结构中发现了问题。 The file created using XCode Interface, result in xml plist file:使用 XCode 接口创建的文件,生成 xml plist 文件:

<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.我修复了 xml 文件中的直接更改,与第一个示例相同,该方法工作正常。

The point is in XCode interface for plist file, you cant see these differences.关键在于plist文件的XCode接口,你看不到这些差异。 The both look the same.两者看起来一样。 Maybe is my Xcode 4.1也许是我的 Xcode 4.1

The correct answer is, the root item IS already an array (in XCode4.. not sure about the others).正确答案是,根项已经是一个数组(在 XCode4 中.. 不确定其他项)。 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.因此,如果您按照neil d在他的屏幕截图中显示的那样放置另一个数组,您最终将得到一个包含两个字符串值的数组的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM