Appkit:如何在本地文件夹上执行 NSMetadataQuery
Appkit: How to perform an NSMetadataQuery on a Local Folder
我正在尝试使用 Appkit 在 本地 macOS 文件夹(不在 iCloud 中)上执行 NSMetadataQuery
:
let query = NSMetadataQuery()
query.valueListAttributes = [ NSMetadataItemURLKey ]
//Set the App's Documents folder as the SearchScope
query.searchScopes = [FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!]
NotificationCenter.default.addObserver(self, selector: #selector(handleQueryNotification), name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: query)
query.enableUpdates()
query.start()
但是,我从未收到查询完成收集结果的通知。当我将 searchScope 更改为 [NSMetadataQueryUbiquitousDocumentsScope]
时,我会收到一条通知。
从Apple's documentation看来应该是可以的:
This array can contain NSURL or NSString objects that represent file-system directories or the search scopes for the query
我做错了什么吗?
您没有指明要查找的内容,但在调用 query.start 之前,您首先必须设置 query.predicate
(根据文档:
https://developer.apple.com/documentation/foundation/nsmetadataquery)
例如:
query.predicate = NSPredicate(format: "%K LIKE '*.txt'", argumentArray: [NSMetadataItemFSNameKey])
我正在尝试使用 Appkit 在 本地 macOS 文件夹(不在 iCloud 中)上执行 NSMetadataQuery
:
let query = NSMetadataQuery()
query.valueListAttributes = [ NSMetadataItemURLKey ]
//Set the App's Documents folder as the SearchScope
query.searchScopes = [FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!]
NotificationCenter.default.addObserver(self, selector: #selector(handleQueryNotification), name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: query)
query.enableUpdates()
query.start()
但是,我从未收到查询完成收集结果的通知。当我将 searchScope 更改为 [NSMetadataQueryUbiquitousDocumentsScope]
时,我会收到一条通知。
从Apple's documentation看来应该是可以的:
This array can contain NSURL or NSString objects that represent file-system directories or the search scopes for the query
我做错了什么吗?
您没有指明要查找的内容,但在调用 query.start 之前,您首先必须设置 query.predicate
(根据文档: https://developer.apple.com/documentation/foundation/nsmetadataquery)
例如:
query.predicate = NSPredicate(format: "%K LIKE '*.txt'", argumentArray: [NSMetadataItemFSNameKey])