writeToFile 输出 .plist 未写入正确的值

发布于 2024-09-06 21:57:04 字数 3005 浏览 5 评论 0原文

我一直在尝试编写一个简单的购物清单程序,但是当我尝试将修改后的值写回 plist 文件时,我只能得到文件中的原始值。

我的原始 plist 文件是一个大型字典数组(dataArray),每个字典中只有两个字段,一个 bool 和一个字符串,当您在表视图中选择一项时,单个项目的 bool 值会更改,当我将 dataArray 输出到控制台我可以看到该值已更改,我唯一的问题是,当我尝试将 dataArray 写入文件时,我在文档目录中获取该文件,但所有布尔值仍然关闭。

- (void)viewDidLoad {

 //load from plist file
 NSString *path = [[NSBundle mainBundle] pathForResource:@"OriginalChecklist" ofType:@"plist"];
 self.dataArray = [NSMutableArray arrayWithContentsOfFile:path];

    [super viewDidLoad];
}

- (void)viewWillDisappear:(BOOL)animated 
{
 NSString *documentsDirectory = [@"~/Documents/" stringByExpandingTildeInPath];
 NSString *savePath = [documentsDirectory stringByAppendingPathComponent:@"Checklist.plist"];
 [dataArray writeToFile:savePath atomically:YES];

 NSLog(@"location information %@", savePath); 
 NSLog(@"dataAray contents %@=", dataArray);
}

正如我所说,当我将 dataArray 的结果输出到控制台时,我从我检查的项目中得到以下结果。

cell = <UITableViewCell: 0x3b143d0; frame = (0 0; 320 44); text = 'Water Bottle'; autoresize = W; layer = <CALayer: 0x3b14650>>;
checked = 1;
text = "Water Bottle";

cell = <UITableViewCell: 0x3b20560; frame = (0 88; 320 44); text = 'GPS'; autoresize = W; layer = <CALayer: 0x3b20610>>;
checked = 1;
text = GPS;

如果有人知道如何将修改后的结果保存到 plist 文件中,我将不胜感激。

谢谢 Brad


我的应用程序在创建 plist 并在 ViewDidLoad 部分中从该 plist 加载信息方面仍然遇到了一些问题。我目前用来检查原始文件的代码,如果不存在,则创建一个如下

(void)viewDidLoad {

BOOL success;

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"CustomChecklist.plist"];

success = [fileManager fileExistsAtPath:filePath];
NSLog(@"STATUS OF SUCCESS %@",success);
if (!success) {
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"OriginalChecklist.plist"];
    success = [fileManager copyItemAtPath:path toPath:filePath];
    self.dataArray = [NSMutableArray arrayWithContentsOfFile:filePath];
    NSLog(@"IF STATEMENT CREATING THE FILE");
}else {
    self.dataArray = [NSMutableArray arrayWithContentsOfFile:filePath];
    NSLog(@"IF STATEMENT READING THE FILE");
}

NSLog(@"location information %@", filePath);
[super viewDidLoad];

}

当我构建此应用程序时,它构建并运行正常,但我收到一些警告 NSFileManager 可能不会响应 -copyItemAtPath:toPath

一旦完成,控制台的输出看起来好像它甚至没有检查文件或创建要读取的文件。

2010-07-06 16:22:42.803 CampingChecklist[400:207] 成功状态(空) 2010-07-06 16:22:42.805 CampingChecklist[400:207] IF 语句读取文件 2010-07-06 16:22:42.814 CampingChecklist[400:207] 位置信息 /Users/Brad/Library/Application Support/iPhone Simulator/3.1.3/Applications/E7074D5A-3E7A-4D2E-9F92-82B1A04873F8/Documents/CustomChecklist .plist

显然,由于文件中没有任何内容,因此当它尝试在退出应用程序的过程中保存时,它不会在指定的路径中创建任何文件。

如果有人能阐明这个问题,我将不胜感激。

谢谢 布拉德

I've been trying to write a simple shopping list program but when i try to write the modified values back to a plist file i only get the original values that were in the file to start with.

My original plist file is one large array (dataArray) of dictionaries with only two fields in each dictionary one bool and one string when you select an item in the tableview the bool value is changed for the single item and when i output the dataArray to the console i can see that the value has been changed, my only problem is that when i try to write dataArray to a file i get the file in the documents directory but all of the bool values are still off.

- (void)viewDidLoad {

 //load from plist file
 NSString *path = [[NSBundle mainBundle] pathForResource:@"OriginalChecklist" ofType:@"plist"];
 self.dataArray = [NSMutableArray arrayWithContentsOfFile:path];

    [super viewDidLoad];
}

- (void)viewWillDisappear:(BOOL)animated 
{
 NSString *documentsDirectory = [@"~/Documents/" stringByExpandingTildeInPath];
 NSString *savePath = [documentsDirectory stringByAppendingPathComponent:@"Checklist.plist"];
 [dataArray writeToFile:savePath atomically:YES];

 NSLog(@"location information %@", savePath); 
 NSLog(@"dataAray contents %@=", dataArray);
}

And as i said when i output the result of the dataArray to the console i get the following results from the items i checked.

cell = <UITableViewCell: 0x3b143d0; frame = (0 0; 320 44); text = 'Water Bottle'; autoresize = W; layer = <CALayer: 0x3b14650>>;
checked = 1;
text = "Water Bottle";

cell = <UITableViewCell: 0x3b20560; frame = (0 88; 320 44); text = 'GPS'; autoresize = W; layer = <CALayer: 0x3b20610>>;
checked = 1;
text = GPS;

If anyone knows how to save the modified results to the plist file i would much appreciate a little help.

Thanks
Brad


I'm still having a little trouble with my application being able to create a plist and load the information from that plist in the ViewDidLoad section. The code i'm using at the moment to check for the original file and if it is not there create one is as follows

(void)viewDidLoad {

BOOL success;

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"CustomChecklist.plist"];

success = [fileManager fileExistsAtPath:filePath];
NSLog(@"STATUS OF SUCCESS %@",success);
if (!success) {
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingFormat:@"OriginalChecklist.plist"];
    success = [fileManager copyItemAtPath:path toPath:filePath];
    self.dataArray = [NSMutableArray arrayWithContentsOfFile:filePath];
    NSLog(@"IF STATEMENT CREATING THE FILE");
}else {
    self.dataArray = [NSMutableArray arrayWithContentsOfFile:filePath];
    NSLog(@"IF STATEMENT READING THE FILE");
}

NSLog(@"location information %@", filePath);
[super viewDidLoad];

}

When I build this application it builds and runs ok but I get a couple of warnings
NSFileManager may not respond to -copyItemAtPath:toPath

Once this has completed the output to the console looks as though it hasn't even checked for the file or created the one to read from.

2010-07-06 16:22:42.803 CampingChecklist[400:207] STATUS OF SUCCESS (null)
2010-07-06 16:22:42.805 CampingChecklist[400:207] IF STATEMENT READING THE FILE
2010-07-06 16:22:42.814 CampingChecklist[400:207] location information /Users/Brad/Library/Application Support/iPhone Simulator/3.1.3/Applications/E7074D5A-3E7A-4D2E-9F92-82B1A04873F8/Documents/CustomChecklist.plist

And obviously as there is nothing in the file, when it tries to save on the way out of the application it doesn't create any file in the path specified.

If anyone can shed any light on this problem then I would much appreciate it.

Thanks
Brad

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悟红尘 2024-09-13 21:57:04

当您第一次加载 plist 时,它来自 App Bundle - 不可写。

当您保存数据时,您已将其正确保存到文档目录中。

但是,当您下次再次加载 plist 时,您将再次从 Appe Bundle 加载它,而该内容尚未更改。

您需要在 plist 加载代码中添加一些逻辑,以检查文档目录中文件是否存在。如果存在,则加载它,如果不存在,则从应用程序包中加载。

When you load your plist for the first time, it comes from the App Bundle - which is not writeable.

When you save your data, you're correctly saving it into the documents directory.

However, when you load your plist again the next time, you're loading it from the Appe Bundle again which hasn't been changed.

You need to put some logic into your plist loading code that checks for the existance of the file in the documents directory. If it exists, load it, if not load the one from the app bundle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文