简体   繁体   中英

Problems when adding key-value pair to plist file in iOS

I have a view controller in an iphone app that adds a key-value pair to a plist file. It then displays the entire plist in a view controller with a table view. The problems I'm having are that it will only display the values in the iPhone simulator and it will only display the new key-value pairs if I completely restart the app.

NOTE: The plist file is supposed to be added to the app's resources directory UPDATE: the code below is the solution to my question, additionally, to view the new entries in the plist, I implemented a refresh button that basically ran the section in viewDidLoad.

The code for the view controller that adds the key-value pair is below:

- (IBAction)addAction:(UIBarButtonItem *)sender
{    
    NSString *myFile=[[NSBundle mainBundle] pathForResource:@"File" ofType:@"plist"];

    dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:myFile];

    NSArray *docDir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *filePath=[docDir objectAtIndex:0];
    NSString *plistPath=[filePath stringByAppendingPathComponent:@"File.plist"];

    //Check plist's existance using FileManager
    NSError *err=nil;
    NSFileManager *fManager=[NSFileManager defaultManager];

    if(![fManager fileExistsAtPath:plistPath])
    {
        //file doesn't exist, copy file from bundle to documents directory

        NSString *bundlePath=[[NSBundle mainBundle] pathForResource:@"File" ofType:@"plist"];
        [fManager copyItemAtPath:bundlePath toPath:plistPath error:&err];
    }

    //Get the dictionary from the plist's path
    dictionary =[[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    //Manipulate the dictionary
    [dictionary setObject:textContent forKey:textName];
    //Again save in doc directory.
    [dictionary writeToFile:plistPath atomically:YES];    
}

This is the code to display the plist in a table view in the other view controller:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Initialize table data

    NSArray *docDir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *filePath=[docDir objectAtIndex:0];
    NSString *plistPath=[filePath stringByAppendingPathComponent:@"File.plist"];


    dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:**plistPath**];
    verses = [dictionary allKeys];

    [self.mainTableView reloadData];

}

Thanks for all your help!

On device the main bundle is read only you want to write to the documents directory path. You can get it via

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

The reason why your changes are only appearing on launch is because the file names don't match

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM