objective-c - NSArray initWithContentsOfFile 返回 nil

标签 objective-c ios5 xcode4 xcode4.2

我正在尝试使用 initWithContentsOfFile 将 plist 文件读入 NSArray,如 here 中所述。

我的代码如下所示

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

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>

This is how it looks in xcode

一切似乎都很好。 plistPath 使用正确的值进行初始化,这意味着文件已复制到包中并且可以读取。但是 routes = [[NSArray alloc] initWithContentsOfFile:plistPath ]; 返回 0x0 值。我认为这等于nil

我的代码有什么问题吗?

最佳答案

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

更改此:

<dict>
    <key>Root</key>
    <array>
        <string>1</string>
        <string>2</string>
    </array>
</dict>

对此:

<array>
    <string>1</string>
    <string>2</string>
</array>

关于objective-c - NSArray initWithContentsOfFile 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10874254/

相关文章:

objective-c - presentViewController 会改变 view.frame 吗?

ios - iPhone 旋转应用于 3d 对象

objective-c - 如何永久更改 NSButton 的光标?

ipad - 如何使用 Storyboard在 4.2 Xcode 中创建自定义模态转场

ios4 - 在 IOS 4.x 或更低版本中,将 subview 添加到 View 时不会调用 viewDidAppear 方法,为什么?

objective-c - Xcode 4 UIButton segue 推送到 TableView Controller

git - 如何阻止 XCode 4 修改本地 Git 存储库?

ios - 删除最后一行后如何重新加载自定义表格 View 单元格?

macos - 如何使用xcodebuild构建特定的体系结构?

ios - 如何让 UIView 在 Interface Builder 中显示其导航 Controller 的导航栏