2

我尝试使用

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

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

我该怎么办?

4

1 回答 1

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

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

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

希望这对所有正在寻找解决方案的人有所帮助:)

于 2011-03-31T12:36:54.457 回答