Appkit:如何在本地文件夹上执行 NSMetadataQuery

问题描述

我正在尝试使用 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 看来这应该是可能的:

这个数组可以包含代表文件系统目录或查询搜索范围的 NSURL 或 Nsstring 对象

我做错了什么吗?

解决方法

您没有指明要查找的内容,但在调用 query.start 之前,您首先必须设置 query.predicate

(每个文档: https://developer.apple.com/documentation/foundation/nsmetadataquery)

例如:

10.0.2.15 
10.0.3.15