我正在尝试从 .plist 文件创建 NSArray,但我不断收到此错误:
" 'NSUnknownKeyException', reason: '[<NSCFString 0x5bbca60> valueForUndefinedKey:]: this class is not key value coding-compliant for the key Value1.'
"
在 .plist 文件中,我有一个名为“Item 1”的键和一个名为“Value1”的字符串值。然后在代码中,我从该文件创建了一个 NSArray:
-(void)recordValues:(id)sender {
// read "propertyList.plist" values
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"propertyList" ofType:@"plist"];
originalArray = [[NSArray alloc] initWithContentsOfFile:plistPath];
// dump the contents of the array to the log
for (id key in originalArray) {
NSLog(@"bundle: key=%@, value=%@", key, [originalArray valueForKey:key]);
}
//create an NSMutableArray containing the
//user's score and the values from originalArray
NSMutableArray *newArray = [NSMutableArray arrayWithObjects:[NSNumber numberWithFloat:userScore], nil];
[newArray addObjectsFromArray:array];
//write xml representation of mutableArray back to propertyList
[newArray writeToFile:plistPath atomically:NO];
}
}