如何删除我的应用程序NSDocumentDirectory下的所有文件?

时间:2011-03-31 12:33:06

标签: iphone ios4 nsdocumentdirectory

我尝试使用

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

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

我该怎么办?

1 个答案:

答案 0 :(得分:6)

 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];

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

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

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