【问题标题】:removeItemAtPath behaving unexpectedlyremoveItemAtPath 行为异常
【发布时间】:2012-12-05 14:21:16
【问题描述】:

我正在尝试使用以下代码从我的应用程序的文档目录中删除一个文件夹:

NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *downloadFolder = [documentsPath stringByAppendingPathComponent:@"downloads"];
NSFileManager *fileManager = [NSFileManager defaultManager];

if ([eraseDevice isEqualToString:@"true"])
{
    if ([fileManager removeItemAtPath:downloadFolder error:NULL] == YES)
        NSLog(@"downloads folder deleted");
    else
        NSLog(@"erase downloads failed");
}

调用时,控制台打印downloads folder deleted,但目录仍然存在。

谁能告诉我上面的代码有什么问题或如何删除有问题的文件夹?谢谢。

-编辑添加documentsPath的NSLogs & downloadFolder-

/Users/User/Library/Application Support/iPhone Simulator/6.0/Applications/6C785921-89B8-49C9-8BFC-06564B95C72C/Documents
/Users/User/Library/Application Support/iPhone Simulator/6.0/Applications/6C785921-89B8-49C9-8BFC-06564B95C72C/Documents/downloads

【问题讨论】:

  • 请打印文档路径和下载文件夹并告诉我输出。
  • removeItemAtPath 它删除文件而不是文件夹。
  • @SmartWork 根据documentation这个方法也删除了目录
  • @tkanzakic:确实,它应该能够删除文件夹和文件。
  • @Robert 我以前用它来正确删除目录,正如@Girish 所说的NSLog documentsPathdownloadFolder 并公开结果,错误可能在那里

标签: objective-c nsdocumentdirectory


【解决方案1】:

1) 目录是空的吗?不确定这是否会有所不同,但只是想检查一下。 2) 当你在removeItemAtPath:error: 行上断点时,你看到文件夹被删除,还是你以后才检查?

【讨论】:

  • 谢谢。原来在下载方法完成之前调用了删除方法。输入条件并解决它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-05
  • 1970-01-01
  • 2017-01-22
  • 2013-04-14
  • 2014-01-29
  • 1970-01-01
  • 2011-01-13
相关资源
最近更新 更多