将IOS数据保存到plist [英] Saving IOS data to plist

查看:302
本文介绍了将IOS数据保存到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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆