在不使用iPhone模拟器的情况下在Plist文件上读/写?

问题描述:

我使用的是来自apress的iphone开发人员手册的示例。 问题是,这个例子只适用于模拟器即时通讯,试图找出我如何使它在设备上工作。本章根本不起作用。下面是示例代码。 data.plist位于资源文件夹中。在不使用iPhone模拟器的情况下在Plist文件上读/写?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
return [documentsDirectory stringByAppendingPathComponent:kFilename]; 

下面然后检查文件是否位于。这是跳过,所以即时猜测这没有找到该文件。

if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]) { 
    NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath]; 
    BGDataSave = [array objectAtIndex:0]; 
    NSLog(@"%@", BGDataSave); 
    price.text = [array objectAtIndex:1]; 
    percent.text = [array objectAtIndex:2]; 
    salepriceLabel.text = [array objectAtIndex:3]; 
    origpriceLabel.text = [array objectAtIndex:4]; 
} 

如果data.plist是在资源目录中,得到这样的路径:

filePath = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; 

如果目的是修改文件,那么你将它复制到文档目录时,不存在。您不能写入存储资源的包中的文件。

+0

我喜欢这个脚本,也许我不是将文件写入文档文件夹我该怎么做? – Silent 2010-06-06 02:16:23

+0

您正在使用NSArray来读取文件。您也可以通过传递要写入的路径来使用它通过'writeToFile:atomically:'编写文件。或者你可以使用NSFileManager来复制文件。 – drawnonward 2010-06-06 04:14:59

你是否首先将文件写入文档目录?如果没有,它不会在那里......