NSArray initWithContentsOfFile返回nil

TOP*_*KEK 2 objective-c xcode4 ios5 xcode4.2

我试图读取plist文件到NSArray使用initWithContentsOfFile的描述在这里

我的代码看起来像这样

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:
                           @"routes" ofType:@"plist" ];
    routes = [[NSArray alloc] initWithContentsOfFile:plistPath ];
    NSLog:(@"contents of myArray %@", routes);
Run Code Online (Sandbox Code Playgroud)

routes.plist 有一个非常简单的数组结构和2个字符串值

<?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>Root</key>
    <array>
        <string>1</string>
        <string>2</string>
    </array>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

这就是它在xcode中的外观

一切似乎都很好.plistPath使用正确的值初始化,这意味着文件被复制到包中并且可以被重新加载.但是routes = [[NSArray alloc] initWithContentsOfFile:plistPath ];回报0x0价值.我认为这等于nil

我的代码出了什么问题?

cal*_*kus 9

实际上你的plist是一本字典.

改变这个:

<dict>
    <key>Root</key>
    <array>
        <string>1</string>
        <string>2</string>
    </array>
</dict>
Run Code Online (Sandbox Code Playgroud)

对此:

<array>
    <string>1</string>
    <string>2</string>
</array>
Run Code Online (Sandbox Code Playgroud)