开发者

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

开发者 https://www.devze.com 2023-02-21 05:38 出处:网络
I tried using NSString *DocumentsDirectoryPath = //Code to fetch the Documents Directory Path; NSError *error;

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 director开发者_C百科y not the Documents directory itself.

What should I do?


 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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号