ios - NSFileManager removeItemAtPath 多次调用时会删除整个文件夹

标签 ios file nsfilemanager

我有以下代码,应该删除 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];

但是,它会删除整个文档文件夹。但是,如果我注释最后三行中的两行,以便它仅删除其中一个文件,则它仅删除该文件。

更新:这是 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 说道:

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

因此 Documents 文件夹被删除。 但我无法解释为什么如果一次只删除一个文件它会起作用。

关于ios - NSFileManager removeItemAtPath 多次调用时会删除整个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14478877/

相关文章:

database - 如何将应用程序包中的数据库文件放入库/应用程序支持/目录中?

ios - 为什么我的 Xcode 知道项目目录应该有这些文件?

ios - 删除第一个注释-MapView iOS

java - 如何读取运行时创建的文件?

c - 逐行读取文件

ios - 仅当文件存在时才显示 cell.button

ios - 更新配置文件

ios - NSPredicate 过滤

file - 来自流的字符

macos - 检查文件是否是 Swift 的别名