我的应用程序不时下载大量 jpeg。所有的 jpeg 文件名都以“hmof-*.jpg”的格式保存有没有办法可以删除所有不以“hmof-”前缀开头的文件?
我显然需要在循环中执行此操作,但我不确定要循环什么。
最佳答案
像这样的……
NSString *bundleRoot = [[NSBundle mainBundle] bundlePath];
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:bundleRoot error:nil];
NSPredicate *fltr = [NSPredicate predicateWithFormat:@"NOT (self BEGINSWITH 'hmof-')"];
NSArray *notHMOF = [dirContents filteredArrayUsingPredicate:fltr];
您可以使用以下方式删除文件:
for (int i=0; i<[notHMOF count]; i++)
{
[fm removeItemAtPath:[NSString stringWithFormat:@"%@/%@",bundleRoot,[notHMOF objectAtIndex:i]] error:nil];
}
编辑:有效..
关于iphone - ios 查找所有保存的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10468440/