arrayWithContentsOfFile 无法读取 plist 文件
我有一个包含 2 个字符串的数组的 plist,请参见图片:
http://imageshack.us/photo /my-images/263/myplist.png/
并在我的 viewDidLoad
中添加以下内容:
NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:file];
NSLog(@"array = %@", array);
但我总是 null
为什么?感谢您的帮助。
更新:解决方案是您需要手动编辑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>
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 = [NSArray 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最有可能的是,您的 plist 是一个包含数组的字典(Xcode 4 不允许创建以数组作为根对象的 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:
在您的应用程序中,您需要更改根键名称,因为它不能是数据类型
in you application you need to change the root key name as it can not be datatype
我研究过这个主题并发现 plist 文件中的一些差异。
例如,我在示例代码中有原始的 plist 文件,可以使用
方法
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];
xml 原始文件是:
当我尝试在 Xcode 界面中使用“添加新文件属性列表”创建上面相同的文件时,并在编辑后以相同的方式放置项目在上面文件的界面中显示,方法
[[NSArray alloc] initWithContentsOfFile:path];
失败,我在 xml plist 的结构中发现了问题 文件。使用 XCode 接口创建的文件,生成 xml plist 文件:
我像第一个示例一样在 xml 文件中直接修复了更改,并且该方法工作正常。
重点是在 plist 文件的 XCode 界面中,你看不到这些差异。两者看起来都一样。也许是我的 Xcode 4.1
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 :
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 :
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
正确的答案是,根项已经是一个数组(在 XCode4 中..不确定其他项)。因此,如果您按照 neil d 的屏幕截图所示放置另一个数组,您最终会得到一个包含具有两个字符串值的数组的数组。
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
:或(基本上两者相同)
可以这样读取:
其中
appFeatures
是plist.
(iOS 6.1)
plist
of this type:OR (basically both are same)
Can be read like this:
Where
appFeatures
is name of theplist
.(iOS 6.1)