arrayWithContentsOfFile 无法读取 plist 文件

发布于 2024-11-13 05:02:27 字数 885 浏览 2 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

む无字情书 2024-11-20 05:02:28

最有可能的是,您的 plist 是一个包含数组的字典(Xcode 4 不允许创建以数组作为根对象的 plist)。尝试这样的事情:

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

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);
真心难拥有 2024-11-20 05:02:28

plist should start with Root Key

在您的应用程序中,您需要更改根键名称,因为它不能是数据类型

plist should start with Root Key

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

下壹個目標 2024-11-20 05:02:28

我研究过这个主题并发现 plist 文件中的一些差异。

例如,我在示例代码中有原始的 plist 文件,可以使用
方法
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];

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>

当我尝试在 Xcode 界面中使用“添加新文件属性列表”创建上面相同的文件时,并在编辑后以相同的方式放置项目在上面文件的界面中显示,方法 [[NSArray alloc] initWithContentsOfFile:path]; 失败,我在 xml plist 的结构中发现了问题 文件。
使用 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>

我像第一个示例一样在 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 :

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

寻梦旅人 2024-11-20 05:02:28

正确的答案是,根项已经是一个数组(在 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.

怀里藏娇 2024-11-20 05:02:28

此类型的 plist

在此处输入图像描述

或(基本上两者相同)

在此处输入图像描述

可以这样读取:

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

其中 appFeaturesplist.
(iOS 6.1)

plist of this type:

enter image description here

OR (basically both are same)

enter image description here

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文