问题描述
限时送ChatGPT账号..我尝试使用 NSMetadataQuery 和 NSPredicate 来搜索文件.经过几个小时的尝试和搜索解决方案(我是 swift 的新手),我有一个小例子.它编译得很好,但结果为零.我尝试了不同的谓词,但最终 metadataQuery.resultCount 始终为 0.任何人都知道出了什么问题?
I try to use NSMetadataQuery and NSPredicate to search for files. After several hours of trying and searching for solutions (I'm new to swift) I have a small example. It compiles fine but the results are zero. I tried different predicates but at the end metadataQuery.resultCount is always 0. Anyone has an idea whats going wrong?
class AppDelegate: NSObject, NSApplicationDelegate {
var metadataQuery: NSMetadataQuery!
var metadataQueryDidUpdateObserver: AnyObject?
var metadataQueryDidFinishGatheringObserver: AnyObject?
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "initalGatherComplete:", name: NSMetadataQueryDidFinishGatheringNotification, object: nil)
metadataQuery = NSMetadataQuery()
metadataQuery.searchScopes = [NSMetadataQueryIndexedLocalComputerScope]
metadataQuery.predicate = NSPredicate(format: "%K LIKE '*'", NSMetadataItemFSNameKey)
metadataQuery.startQuery()
}
func initalGatherComplete(notification: NSNotification) {
metadataQuery.stopQuery()
let resultCounter = metadataQuery.resultCount
NSLog("%lu", resultCounter)
NSNotificationCenter.defaultCenter().removeObserver(self, name: NSMetadataQueryDidFinishGatheringNotification, object: nil)
}
最后但并非最不重要的一点:当前谓词应列出所有文件,但最后谓词应仅列出应用程序.创建这样的谓词的最佳实践是什么?我计划过滤扩展名 .app 但也许有更好的方法?
And last but not least: the current predicate should list all files, but at the end the predicate should only list applications. whats the best practice to create such a predicate? I planned to filter for the extension .app but perhaps there is a better way?
谢谢!
推荐答案
检查您的 谓词语法.LIKE
运算符不适用于 NSMetadataQuery
谓词搜索(实际上 NSPredicate
在Spotlight 元数据搜索的上下文.)
Check your predicate syntax. The LIKE
operator isn't available for NSMetadataQuery
predicate searches (in fact NSPredicate
uses a quite similar but actually different set of operators and behavior in the context of Spotlight metadata searches.)
NSMetadataQuery
的 glob 搜索语法只使用等号:
NSMetadataQuery
's syntax for glob searches just uses an equals sign:
NSPredicate(format: "%K ==[cd] '*'", NSMetadataItemFSNameKey)
这篇关于使用 NSMetadataQuery 搜索文件什么都不做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论