无法读取plist [英] Can't read from plist

查看:190
本文介绍了无法读取plist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些我已经知道该怎么做的事 - 但它没有用。寻找解决这个问题的想法。我认为这与Xcode 4.1创建plist文件的方式有关。

I'm trying to do something that I already know how to do - but it isn't working. Looking for ideas to fix this. I think it's something to do with the way Xcode 4.1 creates plist files.

我正在尝试从plist文件读取字符串,并将它们转储到NSArray中。这是我的代码:

I'm trying to read strings from a plist file, and dump them into an NSArray. This is my code:

NSString *myFile = [[NSBundle mainBundle] 
                                 pathForResource:@"File1"ofType:@"plist"];

myArray = [[NSArray alloc]initWithContentsOfFile: myFile];

我在Xcode中创建了一个plist文件 - 顶层的数组和一些字符串。 myArray始终为null。我尝试在文本编辑器中打开plist,因为我在stackoverflow中的其他地方读到Xcode 4在顶层创建了一个带有Dictionary的plist。我手动编辑使其成为顶层的数组 - 没有成功。最后,我回到了顶层的dict并尝试将其读入NSDictionary - 也没有成功。

I created a plist file in Xcode - an array at the top level and a few strings. myArray is always null. I tried opening the plist in a text editor as I read elsewhere in stackoverflow that Xcode 4 creates a plist with a Dictionary at the top level. I manually edited to make it an array at the top level - no success. Finally, I reverted to the dict at top level and tried to read it into an NSDictionary - also no success.

我在教程后看过教程并阅读了一些类似的关于StackOverflow的问题,但我无法从plist文件中读取任何内容 - 它绝对存在于bundle中。我在Xcode中无数次重新创建了plist。感谢任何建议。

I have watched tutorial after tutorial and read a number of similar questions on StackOverflow but I just can't get anything read in from a plist file - it is definitely there in the bundle. I have recreated the plist countless times in Xcode. Grateful for any suggestions.

这是plist的样子:

This is what the plist looks like:

<?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">
<dict>
<key>New item</key>
<array>
    <string>Ping</string>
    <string>Pang</string>
    <string>Pong</string>
</array>
</dict>
</plist>


推荐答案

你.plist的根对象是 dict ,所以你需要在内存中加载它作为 NSDictionary

The root object of you .plist is a dict, so you need to load it in memory as a NSDictionary.

myDictionary = [[NSDictionary alloc] initWithContentsOfFile: myFile];

这篇关于无法读取plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆