简体   繁体   English

在iOS中将键值对添加到plist文件时出现问题

[英]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. 我在iPhone应用程序中有一个视图控制器,该视图控制器将键值对添加到plist文件中。 It then displays the entire plist in a view controller with a table view. 然后,它将在带有表视图的视图控制器中显示整个plist。 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. 我遇到的问题是,如果我完全重新启动应用程序,它将仅显示iPhone模拟器中的值,并且仅显示新的键值对。

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. 注意:应该将plist文件添加到应用程序的资源目录UPDATE:下面的代码是我的问题的解决方案,此外,为了查看plist中的新条目,我实现了一个刷新按钮,该按钮基本上在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: 这是在另一个视图控制器的表视图中显示plist的代码:

- (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); NSArray路径= NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

The reason why your changes are only appearing on launch is because the file names don't match 之所以只在启动时显示更改,是因为文件名不匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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