0

该类NSMetadataQuery似乎是 Finder/Spotlight 通过元数据搜索文件的方式。

Foundation 框架提供的 NSMetadataQuery 类。查询可以以两种模式运行:异步和带实时更新的异步。第一个简单地对初始搜索时存在的文件执行搜索。后者继续搜索。随着满足或不再满足搜索参数更新的文件更新数据。

https://developer.apple.com/library/content/documentation/Carbon/Conceptual/SpotlightQuery/Concepts/Introduction.html#//apple_ref/doc/uid/TP40001843-BBCFBCAG

但是,它似乎是围绕提供一个目录 ( searchScopes),然后异步返回在这些目录中找到的结果 ( NSMetadataQueryDidFinishGathering)。

我已经有一个包含文件 url 的 NSArray。我想使用与 Spotlight 搜索相同的元数据和查询语法来构建这些 NSURL 的过滤器/搜索。但是我将提供一个文件列表来快速归档,而不是提供一个目录并接收异步结果。

// Something like this...
let imageFileTypePredicate = NSPredicate(fromMetadataQueryString: "(kMDItemGroupId = 13)")
let imageURLs = allURLs.filter{ imageFileTypePredicate.evaluate(with:$0) };

但是,这是使用标准 NSPredicate 搜索而不是文件元数据过滤器并引发错误:

此类与键 _kMDItemGroupId 的键值编码不兼容。

我感兴趣的 Spotlight 元数据属性在此处列出:
https ://developer.apple.com/library/content/documentation/CoreServices/Reference/MetadataAttributesRef/Reference/CommonAttrs.html#//apple_ref/doc/uid /TP40001694-SW1

Spotlight 元数据如何过滤文件 url 数组?

4

1 回答 1

0

为每个 url 创建一个 MDItem 以获取文件的 Spotlight 属性。

MDItem 是一个符合 CF 标准的对象,它表示一个文件以及与该文件关联的元数据。

https://developer.apple.com/reference/coreservices/1658213-mditem

MDItemRef item = MDItemCreateWithURL(kCFAllocatorDefault, url);
CFArrayRef attributes = MDItemCopyAttributeNames(item);
NSDictionary *attributeValues = CFBridgingRelease(MDItemCopyAttributes(item, attributes));
于 2016-12-01T08:05:32.860 回答