我正在尝试使用 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>
一切似乎都很好。 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/