5

在我的缓存目录中,我有一个文件夹名称Example。在那个示例文件夹中,我有子文件夹CSSJS. 现在我想删除CSS子文件夹。我没有得到

我使用了下面的书面代码。如果我使用它,我的整个示例文件夹将被删除。

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *cacheFolderPath = [NSSearchPathForDirectoriesInDomains(NSCacheDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSError *error = nil;

for (NSString *fileName in [fileManager contentsOfDirectoryAtPath:cacheFolderPath error:&error])

{
    [fileManager removeItemAtPath:[cacheFolderPath stringByAppendingPathComponent:fileName] error:&error];

}

谁能帮我。

4

1 回答 1

8
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *cacheFolderPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSError *error = nil;
NSString *dir = [cacheFolderPath stringByAppendingPathComponent:@"yourCSSSUBFolderName"];
[[NSFileManager defaultManager] removeItemAtPath:dir error:nil];

您可以对要删除的任何其他目录执行相同的操作!

稍后,如果您想添加这些文件,而不是首先创建该子文件夹,然后添加您的文件,例如:

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
NSString *directory = [paths objectAtIndex:0];
NSString *dirPath=[directory stringByAppendingPathComponent:@"yourCSSSUBFolderName"];
NSFileManager *fileManger=[NSFileManager defaultManager];
if(![fileManger fileExistsAtPath:dirPath])
{
    NSError *error = nil;
    [fileManger createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
}
// now add the directories
NSString *filePath = [dirPath stringByAppendingPathComponent:@"Yourfile.extension"];
[yourData writeToFile:filePath atomically:YES]; //Write to file
于 2012-12-14T05:33:54.250 回答