如何写入plist文件? [英] How can I write to a plist file?

查看:48
本文介绍了如何写入plist文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了这小段代码以写入plist文件.我没有从XCode收到任何错误或警告,所以我真的迷路了.此代码节中未定义根",但这只是我从plist中提取的数据的字典.

I wrote this small bit of code to write to a plist file. I am not getting any errors or warnings from XCode, so I am really lost. "Root" is not defined in this code section, but it is just a dictionary of the data that I pulled from the plist.

有人知道为什么不将其写入plist文件吗?它实际上根本不会更改文件.

Does anyone have an idea of why this would not write to a plist file? It literally does not change the file at all.

    NSMutableArray *newReports = [[NSMutableArray alloc] init];
    newReports = [[Root objectForKey:@"Reports"] mutableCopy ];

    //Creating data model for new report
    NSMutableDictionary *newReport = [[NSMutableDictionary alloc] init];

    NSString *tempTitle = titleField.text;
    NSString *tempEmployee = employeeField.text;
    NSArray *tempNodes = [[NSArray alloc] init];

    [newReport setObject:tempTitle forKey:@"Title"];
    [newReport setObject:tempEmployee forKey:@"Employee"];
    [newReport setObject:tempNodes forKey:@"Nodes"];

    [newReports addObject:newReport];

    NSMutableDictionary *newRoot = [[NSMutableDictionary alloc] init];

    [newRoot setObject:newReports forKey:@"Reports"];

    //Writing data to plist
    NSString *Path = [[NSBundle mainBundle] bundlePath];
    NSString *filePath = [Path stringByAppendingPathComponent:@"data.plist"];

    [newRoot writeToFile:filePath atomically: YES];

感谢您的帮助!

推荐答案

尝试像这样构建文件路径

try constructing your file path like this

NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectoryPath 
                         stringByAppendingPathComponent:@"data.plist"];
[newRoot writeToFile:filePath atomically: YES]; 

这篇关于如何写入plist文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆