写入文档目录中的 plist Xcode

writing to a plist in documents directory Xcode

我有一个应用程序,我将一个 plist 从主包复制到我用来填充分段 UITableview 的文档目录。这一切都很好。我想在用户输入时更改 plist 中的一些信息(部分的 header 名称)和(单元格的详细文本)。我还想添加额外的部分,复制那里的部分,并在用户输入时输入信息。这是 plist

我已经查找了这方面的信息并尝试了各种方法,但似乎没有任何效果。有人可以告诉我如何更改 header 字符串以及如何添加详细文本并添加带有 header 的全新部分和带有项目的行吗?

这是我用来尝试更新 header 文件但没有成功的代码

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

如有任何帮助,我们将不胜感激。

截至目前.. plist 通常用来管理持久化数据存储的索引文件。所以你可以读一次然后写回来。但您不能以编程方式创建新条目。如果数据是动态的,请使用核心数据实体。