我正在尝试使用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
我的代码出了什么问题?
发布于 2012-06-04 06:02:04
实际上你的plist是一本字典。
更改此设置:
<dict>
<key>Root</key>
<array>
<string>1</string>
<string>2</string>
</array>
</dict>要这样做:
<array>
<string>1</string>
<string>2</string>
</array>发布于 2012-06-04 06:02:22
我认为你想做这样的事情:
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:
@"routes" ofType:@"plist"];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSArray *routes = [dictionary objectForKey:@"Root"];https://stackoverflow.com/questions/10874254
复制相似问题