Search code examples
uitableviewios7plistnsdocumentdirectory

writing to a plist in documents directory Xcode


I have an app which I copy a plist from the main bundle to the documents directory which I use to populate a sectioned UITableview. All this works well. I want to change some of the information in the plist as the users enters it( the header name of the section) and (the detailed text of the cells). I also want to add additional sections duplicating the section that is there and putting in information as the user enters it. this is the plistenter image description here

I have looked for information on this and tried various things but nothing seems to work. Can someone show me how to change the header string and how to add the detailed text and add a whole new section with header and rows with items?

this is the code I've used to try to update the header file with no luck

 NSArray *documentPaths15 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory18 = [documentPaths15 objectAtIndex:0];
    NSString *path10 = [documentsDirectory18  stringByAppendingPathComponent:@"TableDataMainView.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSMutableDictionary *data1;

    if ([fileManager fileExistsAtPath: path10])
    {
        data1 = [[NSMutableDictionary alloc] initWithContentsOfFile: path10];  // if file exist at path initialise your dictionary with its data
        NSLog(@"file exists");
    }
    else
    {
        // If the file doesn’t exist, create an empty dictionary
        data1 = [[NSMutableDictionary alloc] init];
        NSLog(@"file doesnt exist");
    }

    //To insert the data into the plist

    [data1 setObject:[NSString stringWithString:yourName] forKey:@"header"];
    [data1 writeToFile: path10 atomically:YES];

Any help would be greatly appreciated.


Solution

  • As of Now.. plist usually used to manage persistence data store index file. So you can read it once and write it back. but you can not create new entry programatically. use core data Entity if you data is dynamic.