开发者

Creating new dictionary when saving data to plist

So I am saving 3 NSStrings from 3 UITextFields to a property list. This works fine, but everytime I save something new, the app overwrites the data that was saved before. So basically there is only 1 Dictionary used, but i want the app to create a new dictionary everytime i save something new, so that no data gets deleted. I have no Idea how i could do this, so please help me!! :)

Code:

NSMutableArray *array = [NSMutableArray arrayWithCapacity:3];
        NSArray *keys = [NSArray arrayWithObjects:@"1",@"2",@"3", nil]; 
        [array addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@",lab.text],[NSString stringWithFormat:@"%@",lab1.text],[NSString stringWithFormat:@"%@",lab2.text], nil] forKeys:开发者_开发知识库keys]]; 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
        [array writeToFile:path atomically:YES];


Use a NSMutableArray to which you add each new dictionary object and then write that array to data.plist.


Is there any reason why you alloc the array with capacity? I would just use [[NSMutableArray alloc] init], then add your objects.

Also, I had trouble saving NSMutableDictionaries in the NSUserDefaults, so what I ended up doing was just saving the dictionary to a file with

[dict writeToFile:filePath atomically:NO];

and initWithContentsOfFile or initWithContentsOfURL depending if I wanted to load a local or Internet file.

I should add, you can writeToFile, initWithContentsOf* for NSMutableArray as well.


Ok I've got it:

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

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];

    NSLog(@"path='%@'",path);

    NSFileManager *nfm = [[NSFileManager alloc] init];
    if([nfm fileExistsAtPath:path])
    {

        // if file exists, get its contents, add more entries and write back

        NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];
        NSArray *keys = [NSArray arrayWithObjects:@"4",@"5",@"6",nil]; 
        [array addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@",lab.text],[NSString stringWithFormat:@"%@",lab1.text],[NSString stringWithFormat:@"%@",lab2.text], nil] forKeys:keys]]; 
        NSLog(@"modified array=%@",array);
        BOOL ok = [array writeToFile:path atomically:YES];
        if(!ok){
            NSLog(@"Unable to write appended file");
            return;
        }

    } else {

        // if file doesn't exist, create a new one

        NSMutableArray *array = [[NSMutableArray alloc] init];
        NSArray *keys = [NSArray arrayWithObjects:@"1",@"2",@"3",nil]; 
        [array addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSString stringWithFormat:@"%@",lab.text],[NSString stringWithFormat:@"%@",lab1.text],[NSString stringWithFormat:@"%@",lab2.text], nil] forKeys:keys]]; 
        NSLog(@"new array=%@",array);
        BOOL ok = [array writeToFile:path atomically:YES];
        if(!ok){
            NSLog(@"Unable to write new file");
            return;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜