我正在尝试将 plist 文件中的数据解析为 NSMutableArray。
在我的 plist Root 中,是一个包含 6 个数字数组的字典
我创建了一个与 IBOutlet UILabel *lbl4 对象挂钩的标签,我希望该标签显示读取 plist 的数组的第一个元素。问题是程序在分配指令(最后一条)处崩溃。我的代码是这样的:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [paths objectAtIndex:0];
NSString *plistPath = [docPath stringByAppendingPathComponent:@"settings.plist"];
if(![[NSFileManager defaultManager] fileExistsAtPath:plistPath]);
{
plistPath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
}
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *err = nil;
NSPropertyListFormat format;
NSDictionary *temp = (NSDictionary *) [NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&err];
if(!temp)
{
NSLog(@"Error reading plist: %@, format: %d", err, format);
}
self.dataSet = [NSMutableArray arrayWithArray:[temp objectForKey:@"Dadi"]];
[lbl4 setText:[NSString stringWithFormat:@"%@", [dataSet objectAtIndex:0]]];
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>Dadi</key>
<array/>
<key>D4</key>
<integer>0</integer>
<key>D6</key>
<integer>0</integer>
<key>D8</key>
<integer>0</integer>
<key>D10</key>
<integer>0</integer>
<key>D12</key>
<integer>0</integer>
<key>D20</key>
<integer>0</integer>
</dict>
</plist>
调试输出显示“2012-09-02 18:29:55.483 Faith[6014:707] * 由于未捕获的异常 'NSRangeException' 而终止应用程序,原因:'* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array '"