How to delete all the files under my app's NSDocumentsDirectory?

时光毁灭记忆、已成空白 提交于 2019-12-06 09:46:42

问题


I tried using

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

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.

What should I do?


回答1:


 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:

How to delete the contents of the Documents directory (and not the Documents directory itself)?

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



来源:https://stackoverflow.com/questions/5499903/how-to-delete-all-the-files-under-my-apps-nsdocumentsdirectory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!