Objective c 写入Plist文件,但不要替换它
我正在创建一个将字符串写入plist文件的应用程序,但我遇到的问题是,每次它写入plist文件时,它都会删除上一个文件,我试图找出如何在不删除原始内容的情况下写入现有文件,或者替换plist文件并保留原始内容,然后将其重新写入 下面是我保存文件的代码Objective c 写入Plist文件,但不要替换它,objective-c,ios,string,nsstring,Objective C,Ios,String,Nsstring,我正在创建一个将字符串写入plist文件的应用程序,但我遇到的问题是,每次它写入plist文件时,它都会删除上一个文件,我试图找出如何在不删除原始内容的情况下写入现有文件,或者替换plist文件并保留原始内容,然后将其重新写入 下面是我保存文件的代码 - (NSString *) saveFilePath { NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMa
- (NSString *) saveFilePath
{
NSArray *pathArray =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:@"scores.plist"];
}
-(void)alertView:(UIAlertView *)alert_view didDismissWithButtonIndex:
(NSInteger)button_index{
if(button_index == 0){
NSLog(@"1");
score = 0;
}
if(button_index == 1){
NSLog(@"2");
NSString *scoreString = [NSString stringWithFormat:@"%i by %@", score, name.text];
NSLog(@"%@", scoreString);
NSArray *values = [[NSArray alloc] initWithObjects:scoreString, nil];
[values writeToFile:[self saveFilePath] atomically:YES];
[values release];
score = 0;
}
}
有什么想法吗?谢谢 您可以读取plist并将其写入NSMutableArray。然后将其与数据一起追加,并将其写回文件,覆盖现有文件
NSMutableDictionary也是如此。因此基本上创建一个NSArray,将现有的plist文件内容添加到其中,并将新内容添加到其中,然后保存?NSMutableArray*arr=[NSMutableArray arrayWithContentsOfFile:file_path];是的,把它读入字典,修改它,然后写回同一个文件。