问题描述
我有一个/ Documents / Images文件夹,在那个文件夹中有几个名为多年的文件夹,在那些文件夹中我有图像。我要删除图像文件夹中的所有内容(包括文件夹和这些文件夹中的图像)。
I have a /Documents/Images folder , in that folder are several other folders named after years , in those folders i have images. I want to delete everything from the images folder ( including the folders and the images in these folders).
我尝试了几个代码片段,但没有删除文件夹
I tried several code snippets but none delete the folders
我的方法:
- (void) clearCache:(NSString *) folderName{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSFileManager *fm = [NSFileManager defaultManager];
NSString *directory = [documentsDirectoryPath stringByAppendingPathComponent:folderName];
NSLog(@"Removing items at path: %@",directory);
NSError *error = nil;
BOOL succes = [fm removeItemAtPath:directory error:&error];
/*for (NSString *file in [fm contentsOfDirectoryAtPath:directory error:&error]) {
//BOOL success = [fm removeItemAtPath:[NSString stringWithFormat:@"%@%@", directory, file] error:&error];
BOOL success = [fm removeItemAtPath:[directory stringByAppendingPathComponent:file] error:&error];
if (!success || error) {
// it failed.
}
}*/
}
推荐答案
您的代码( [fm removeItemAtPath:directory error:& error];
)应该这样做。如果没有,检查它返回的错误。如果没有错误,但您仍然看到文件/子文件夹 - 提交错误报告!
Your code ([fm removeItemAtPath:directory error:&error];
) should do it. If it doesn't, inspect the error it returns. If there's no error, but you still see files/subfolders - file a bug report!
这篇关于从某个文件夹中删除所有文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!