Writing a new set of data to Plist instead of overwriting it

浪子不回头ぞ 提交于 2019-11-29 23:37:59

问题


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?


回答1:


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

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.

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

Here is the modified code

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];



回答2:


instead of [friend setValue:array forKey: @"Friend Data"]; use a unique key in place of @"Friend Data", like the friend name+surname (at least...), or you'll have each time a single entry in your dictionary.



来源:https://stackoverflow.com/questions/8816792/writing-a-new-set-of-data-to-plist-instead-of-overwriting-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!