不断弹出一个错误,说明键的数量与对象的数量不同,并且程序不断崩溃。
下面的代码是有问题的。
-(void)saveData //error is in here
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//get documents path
NSString *documentsPath = [paths objectAtIndex:0];
//get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
//set the variables to the values in the text fields
self.topScores = highScore.text;
//create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: topScores, nil] forKeys:[NSArray arrayWithObjects: @"bestScore", nil]];
NSString *error = nil;
//create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList: plistDict format: NSPropertyListXMLFormat_v1_0 errorDescription: &error];
//check if plist data exists
if (plistData)
{
//write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
}
else
{
NSLog(@"Error in saveData: %@", error);
[error release];
}
}
该代码有效,但随后在此处崩溃
if (newTopScore > [topScores intValue])
{
topScores = ([NSString stringWithFormat:@"%i", newTopScore]);
}
highScore.alpha = 1;
[self saveData];