简体   繁体   English

无法从plist读取

[英]Can't read from 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. 我认为这与Xcode 4.1创建plist文件的方式有关。

I'm trying to read strings from a plist file, and dump them into an NSArray. 我正在尝试从plist文件读取字符串,并将其转储到NSArray中。 This is my code: 这是我的代码:

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

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

I created a plist file in Xcode - an array at the top level and a few strings. 我用Xcode创建了一个plist文件-一个顶级数组和一些字符串。 myArray is always null. myArray始终为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. 我尝试在文本编辑器中打开plist,正如我在stackoverflow的其他地方阅读的那样,Xcode 4在顶层创建了一个带有Dictionary的plist。 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. 最后,我回到了顶层的dict上,试图将其读入NSDictionary-也没有成功。

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. 我看了一个又一个的教程,并在StackOverflow上阅读了许多类似的问题,但我只是无法从plist文件中读取任何内容-它肯定在软件包中。 I have recreated the plist countless times in Xcode. 我已经在Xcode中无数次地重新创建了plist。 Grateful for any suggestions. 感谢任何建议。

This is what the plist looks like: 这就是plist的样子:

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

If you just want the plist loaded, you need to use property list serialization to first load the plist as NSData 如果只想加载plist,则需要使用属性列表序列化首先将plist加载为NSData。

[[NSData alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"File1"]]

then serialize it into a dict (or array, depending on what your root item is) using 然后使用以下命令将其序列化为dict(或数组,具体取决于您的根项)

NSDictionary *aDict = [NSPropertyListSerialization propertyListWithData: options: format: error:]

When you view the plist in Xcode, the top level element is the type to make aDict. 当您在Xcode中查看plist时,顶级元素是制作aDict的类型。

See here for more info: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/SerializePlist/SerializePlist.html 请参阅此处以获取更多信息: http : //developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/SerializePlist/SerializePlist.html

If you want the entire plist loaded as strings, you probably need to do something with an NSString method, something like [NSString stringFromData] . 如果要将整个plist作为字符串加载,则可能需要使用NSString方法执行某些操作,例如[NSString stringFromData]

.plist的根对象是dict ,因此您需要将其作为NSDictionary加载到内存NSDictionary

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

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

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