写入 Plist 文件,但不替换它
我正在创建一个将字符串写入 plist 文件的应用程序,但我遇到的问题是每次写入 plist 文件时,它都会删除前一个文件,我试图弄清楚如何写入现有文件而不删除其原始文件内容,或替换 plist 文件并保留原始内容,然后将它们重新写入其中。
这是我保存文件的代码的样子有
- (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;
}
}
什么想法吗?谢谢!
Im creating a application that writes strings to a plist file, but the problem im having is every time its writing to the plist file, it deletes the previous one, im trying to figure out either how to write to the existing one without deleting its original contents, or replace the plist file and keep the original contents and then re write them on to it..
Heres what my code looks like to save the file
- (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;
}
}
Any ideas? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以读取 plist 并将其写入 NSMutableArray。然后将您的数据附加到该文件中,并将其写回到文件中,覆盖现有文件。
NSMutableDictionary 也是如此。
You can read the plist and write it to the NSMutableArray. Then append it with your data and write it back to the file overwriting the existing one.
The same thing with NSMutableDictionary.