将IOS数据保存到plist [英] Saving IOS data to plist
问题描述
我正在将这些数据写入plist文件,但是每次写入时,它都会替换以前的内容.也就是说,它会覆盖我重新加载页面时以前拥有的所有数据.我如何才能保留以前的数据,或者有人可以指出我的操作不正确?
I am writing these data to plist file, but every time I write, it replaces whatever was there before. I.e it is overwriting whatever data I had previously when I reload the page. How can I make the previous data persist or could someone point out what I am not doing correctly?
- (IBAction)buttonClickSave:(id)sender;
{
pathList=[self getFilePath];
if(![[NSFileManager defaultManager] fileExistsAtPath:pathList])
{
[[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] pathForResource:@"adresses" ofType:@"plist" ]toPath:pathList error:nil];
}
NSString *Name = @"Names";
NSString *email = @"email";
NSString *phone = @"phone";
NSMutableArray *values = [[NSMutableArray alloc] initWithObjects:Name, email, phone, nil];
NSMutableArray *keys = [[NSMutableArray alloc] initWithObjects:kTitleKey, kEmail, kPhone, nil];
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
[self.arraysData addObject:dict];
[self.arraysData writeToFile:pathList atomically:YES];
}
推荐答案
要初始化数组,请使用现有文件作为其源.这样可以确保您添加的所有条目都不会覆盖,而是对现有数据进行更改.
To initialize the array, use the existing file as its source. This will ensure that whatever entries you are adding is not overwriting but making changes to existing data.
因此,无论您在初始化arraysData
的何处,都应使用以下代码:
So, wherever you are initializing the arraysData
, use following code:
arraysData = [NSMutableArray arrayWithContentsOfFile: [self getFilePath]];
这篇关于将IOS数据保存到plist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!