将一组新数据写入Plist而不是覆盖它 [英] Writing a new set of data to Plist instead of overwriting it

查看:122
本文介绍了将一组新数据写入Plist而不是覆盖它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用plist来存储多组数据,但每次保存时(使用ActionSheet中的按钮),它都会覆盖前一组。我想添加多个朋友及其数据。我不太热衷于使用核心数据,所以我想知道如何使用Plist。

I'm trying to get a plist to store multiple sets of data, but each time I save (using a button from an ActionSheet), it overwrites the previous set. I want to add multiple 'friends' and their data. I'm not too keen on using Core Data, so I'm wondering how you can do it with a Plist.

以下是保存按钮的代码:

Here is the code for the save button:

        NSMutableDictionary *friend = [[NSMutableDictionary alloc] init];

        NSMutableDictionary *array = [[NSMutableDictionary alloc]init];
        [array setObject:friendName.text forKey:@"Name"];
        [array setObject:friendPhone.text forKey:@"Phone Number"];
        [array setObject:friendEmail.text forKey:@"Email Address"];
        [array setObject:friendChatSN.text forKey:@"Chat Screen Name"];

        [friend setValue:array forKey: @"Friend Data"];


        NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *documentsPath = [paths objectAtIndex:0];

        NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"FriendList.plist"];

        [friend writeToFile:plistPath atomically: TRUE];

我怎样才能修改代码,每次都添加一组新的朋友数据我保存?

Instead of overwriting it, how can I modified the code to add a new set of friend data each time I save?

推荐答案

这里pList表现为一个对象。因此,每当你尝试写一些东西到plist时,它将被替换为新数据。

Here pList behave as an one object. so whenever you try to write something to plist it will be replaced with new data.

看起来你想要将多个朋友记录保存到plist文件。您的问题的最佳解决方案是使用核心数据或sqlite。但是如果你想用plist实现它,那么首先从plist获取旧数据并将新数据(朋友)添加到旧数据然后保存到plist应该可以解决。

Look like you want to save multiple friends record to plist file. Best solution for your question is to use core data or sqlite. But if you want to achieve it using plist, then first get old data from plist and the add new data (friends) to old data then saving to plist should work out.

根据您的要求,您应该使用多个 NSDictionary NSArray c>(朋友记录)对象到pList。

As per your requirement you should save NSArray with multiple NSDictionary(friend record) object to pList.

这是修改后的代码

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsPath = [paths objectAtIndex:0];

    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"FriendList.plist"];

    NSMutableArray *friend = [NSMutableArray arrayWithContentsOfFile:plistPath];

    if (nil == friend) {
        friend = [[NSMutableArray alloc] initWithCapacity:0];
    }
    else {
        [friend retain];
    }


    NSMutableDictionary *array = [[NSMutableDictionary alloc]init];
    [array setObject:friendName.text forKey:@"Name"];
    [array setObject:friendPhone.text forKey:@"Phone Number"];
    [array setObject:friendEmail.text forKey:@"Email Address"];
    [array setObject:friendChatSN.text forKey:@"Chat Screen Name"];

    [friend addObject:array];
    [array release];



    [friend writeToFile:plistPath atomically: TRUE];
    [friend release];

这篇关于将一组新数据写入Plist而不是覆盖它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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