如何删除我的应用程序的NSDocumentsDirectory下的所有文件? [英] How to delete all the files under my app's NSDocumentsDirectory?

查看:106
本文介绍了如何删除我的应用程序的NSDocumentsDirectory下的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用

NSString *DocumentsDirectoryPath = //Code to fetch the Documents Directory Path;
NSError *error;
NSFileManager *manager = [NSFileManager defaultManager];
[manager removeItemAtPath:DocumentsDirectoryPath error:&error];

上面的代码删除了我的应用程序的整个Documents目录,但我只想删除文档目录的内容 文件目录本身。

The above code deletes the whole Documents directory of my app, but I just want to delete contents of the Documents directory not the Documents directory itself.

我该怎么办?

推荐答案

 NSString *docsDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
 NSFileManager *localFileManager=[[NSFileManager alloc] init];
 NSDirectoryEnumerator *dirEnum = [localFileManager enumeratorAtPath:docsDir];

 NSString *file;
 NSError *error;
 while ((file = [dirEnum nextObject])) 
 {
     NSString *fullPath = [NSString stringWithFormat:@"%@/%@", docsDir,file];
     // process the document
     [localFileManager removeItemAtPath: fullPath error:&error ];
 }
 [localFileManager release];

请参阅此链接以获取答案:

Refer to this link for the answer:

如何删除Documents目录的内容(而不是Documents目录本身)?

希望这有助于所有寻求解决方案的人: )

Hope this helps all those who are looking out for a solution to this :)

这篇关于如何删除我的应用程序的NSDocumentsDirectory下的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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