【发布时间】:2012-10-31 06:41:49
【问题描述】:
我使用以下代码将值写入字典,但是当将新值添加到字典时它不会更新,它只会显示只有最近添加的值的 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 文件。
【问题讨论】:
-
在你的代码中删除这段代码 dict=[[NSMutableDictionary alloc]init];[dict release];,它会起作用