【问题标题】:Objective c : problem with directory file目标c:目录文件问题
【发布时间】:2011-05-20 17:01:19
【问题描述】:

我有这个代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"File.plist"]; 


if([array writeToFile:path atomically: YES]){
    NSLog(@"write succesful");}
else {
    NSLog(@"write failed");
}

我在 Resources 文件夹中创建了一个文件“File.plist”,我想在其中存储一个 NSMutableArray。当我调用此方法时,控制台中的消息是“写入成功”,但如果我更改路径

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"FileAbstract.plist"];

消息总是“写入成功?为什么?我在路径中更改了名称。

【问题讨论】:

  • 看起来您创建了第二个文件,您查看目录了吗?
  • 如何查看目录?
  • 您可以在模拟器文件夹中找到该应用程序,它通常位于~/Library/Application Support/iPhone Simulator/4.3/Applications
  • 然后使用NSFileManager类的contentsOfDirectoryAtPath:error:方法。

标签: objective-c ios nsmutablearray plist


【解决方案1】:

在此代码中,您在文档目录中编写,该目录与项目中的资源文件夹无关。您无法通过运行应用程序来更改资源文件夹中的文件。在第一次运行中,您编写了文件File.plist,在第二次运行中,您编写了文件FileAbstract.plist,两者都在文档目录中。它们与资源文件夹中的File.plist 无关。

编辑:读取数组中文件的内容:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"File.plist"];

NSArray *array = [NSArray arrayWithContentsOfFile:path];

【讨论】:

  • 啊好的我明白了,我可以看到 File.plist 吗?
  • 在模拟器中,您可以轻松检查文件。在设备上,我听说可以通过 iTunes 同步/备份应用程序数据,但我自己从未尝试过。所以无法确认。您可以使用方法 arrayWithContentsOfFile:path 创建一个 NSArray 并检查数组的内容。
猜你喜欢
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多