【问题标题】:NSFileManager removeItemAtPath deletes the entire folder when called more than onceNSFileManager removeItemAtPath 在多次调用时删除整个文件夹
【发布时间】:2013-01-06 21:11:21
【问题描述】:

我有以下代码应该删除 3 个文件:

NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *pngFilePath = [docDir stringByAppendingPathComponent:currentFileName];
NSLog(@"png %@",pngFilePath);

NSString *thumbFilePath = [docDir stringByAppendingPathComponent:currentThumbFileName];
NSLog(@"thumb %@",thumbFilePath);

NSString *plistFilePath = [docDir stringByAppendingPathComponent:currentPlistName];
NSLog(@"plist %@",plistFilePath);

NSError *error, *error2, *error3;
[[NSFileManager defaultManager] removeItemAtPath:pngFilePath error:&error];
[[NSFileManager defaultManager] removeItemAtPath:thumbFilePath error:&error2];
[[NSFileManager defaultManager] removeItemAtPath:plistFilePath error:&error3];

但是,它会删除整个 Documents 文件夹。但是如果我注释了最后三行中的两行,所以它只删除一个文件,它只删除那个文件。

更新:这是 NSLog:

2013-01-23 13:51:28.715 Amaziograph[16466:c07] png /Users/Hristo/Library/Application Support/iPhone Simulator/5.1/Applications/C9FAA196-7904-4070-A208-A53451A64602/Documents/amaziograph_2013_01_23_13_43_37.png
2013-01-23 13:51:28.716 Amaziograph[16466:c07] thumb /Users/Hristo/Library/Application Support/iPhone Simulator/5.1/Applications/C9FAA196-7904-4070-A208-A53451A64602/Documents/amaziograph_2013_01_23_13_43_37_thumb.png
2013-01-23 13:51:28.716 Amaziograph[16466:c07] plist /Users/Hristo/Library/Application Support/iPhone Simulator/5.1/Applications/C9FAA196-7904-4070-A208-A53451A64602/Documents/amaziograph_2013_01_23_13_43_37.plist

如何删除所有 3 个?

【问题讨论】:

  • 可能是您的文件夹和文件名相同。
  • 你能发布 NSLog 调用的输出吗?

标签: ios file nsfilemanager


【解决方案1】:

原来我调用了上面的代码两次——第一次没有特定的文件名,所以它删除了文件的文件夹。 NSLog 说:

png /Users/... .../Documents/(null).png

所以 Documents 文件夹被删除了。 但是我无法解释为什么一次只删除一个文件会起作用。

【讨论】:

  • 删除前检查文件名是否为空。
猜你喜欢
  • 1970-01-01
  • 2011-06-05
  • 2012-01-23
  • 2011-04-05
  • 2012-05-23
  • 1970-01-01
  • 1970-01-01
  • 2011-11-21
  • 1970-01-01
相关资源
最近更新 更多