如何删除Documents目录的内容(而不是Documents目录本身)? [英] How to delete the contents of the Documents directory (and not the Documents directory itself)?

查看:302
本文介绍了如何删除Documents目录的内容(而不是Documents目录本身)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除Documents目录中包含的所有文件和目录。

I want to delete all the files and directories contained in the Documents directory.

我相信使用 [fileManager removeItemAtPath:documentsDirectoryPath error:nil] 方法也会删除文档目录。

I believe using [fileManager removeItemAtPath:documentsDirectoryPath error:nil] method would remove the documents directory as well.

是否有任何方法可以删除目录中的内容并将空目录留在那里?

Is there any method that lets you delete the contents of a directory only and leaving the empty directory there?

推荐答案

试试这个:

NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSError *error = nil;
for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error]) {
    [[NSFileManager defaultManager] removeItemAtPath:[folderPath stringByAppendingPathComponent:file] error:&error];
}

这篇关于如何删除Documents目录的内容(而不是Documents目录本身)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆