我正在尝试创建一个搜索功能,搜索挂载的磁盘及其所有子文件夹。我用来测试的磁盘没有很多子目录(最多3-4个?),当前的搜索过程大约需要5秒左右才能完成。这不是世界末日,但我希望它运行得更顺畅一些。
下面是我当前使用的代码:
NSMutableArray *testFiles = [[NSMutableArray alloc] init];
NSFileManager *localFileManager=[[NSFileManager alloc] init];
NSDirectoryEnumerator *dirEnum =
[localFileManager enumeratorAtPath:@"/volumes/"];
NSString *filename = (NSString *)dirEnum;
while ((filename = [dirEnum nextObject])) {
if ([filename rangeOfString:@"stringName" options:NSCaseInsensitiveSearch].length != 0) {
[testFiles addObject:[@"/Volumes/" stringByAppendingPathComponent:filename]];
NSLog(@"%@",testFiles);
}
}
}
查看NSFileManager中的以下内容:
mountedVolumeURLsIncludingResourceValuesForKeys:选择:
这将获得所有卷的列表,因此您可以选择需要的卷。可以选择忽略隐藏卷(例如隐藏的Time Machine卷)。
enumeratorAtURL: includingPropertiesForKeys:选择:errorHandler:
不同的枚举数。具有允许跳过隐藏文件的选项,重要的是可以跳过包的后代。例如,应用程序包中的所有文件都不会被枚举。
在NSDirectoryEnumerator中,检查你是否可以对一些你不关心的目录调用skipDescendants。
或者用完全不同的方法,使用Spotlight。