我使用以下代码将值写入字典,但是当将新值添加到字典中时,它不会更新,它只显示最近添加的值的 plist,它也崩溃了。
nameString=nameTxt.text;
NSFileManager *mngr=[NSFileManager defaultManager];
NSArray *docDir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath=[docDir objectAtIndex:0];
NSString *filePath=[docPath stringByAppendingPathComponent:@"score.plist"];
NSString *bundlePath=[[NSBundle mainBundle] pathForResource:@"score" ofType:@"plist"];
if ([mngr fileExistsAtPath:filePath]) {
NSLog(@"File exists");
}
else {
NSLog(@"NO file exists");
[[NSFileManager defaultManager] copyItemAtPath:bundlePath toPath:filePath error:NULL];
}
dict=[[NSMutableDictionary alloc]init];
dict=[NSMutableDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"dict is %@",dict);
[dict setObject:nameString forKey:@"100"];
[dict writeToFile:filePath atomically:YES];
[dict release];
当我使用最后一行“[dict release]”时我崩溃了我的包中有一个 score.plist 文件。