【问题标题】:Appkit: How to perform an NSMetadataQuery on a Local FolderAppkit:如何在本地文件夹上执行 NSMetadataQuery
【发布时间】:2021-03-01 13:59:49
【问题描述】:

我正在尝试使用这样的 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 对象

是不是我做错了什么?

【问题讨论】:

    标签: swift macos appkit


    【解决方案1】:

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

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

    例如:

            query.predicate = NSPredicate(format: "%K LIKE '*.txt'", argumentArray: [NSMetadataItemFSNameKey])
    

    【讨论】:

    • 感谢您的帮助。现在我确实得到了一个结果,但它始终是一个空数组,即使我在文件夹中存放了 soms .txt 文件。
    • 我尝试了不同的 nspredicates,但我不断得到一个空数组,所以可能还有另一个问题。
    • 这是我正在玩的文件,以防它可能有助于孤立:gist.github.com/dldnh/819f11633359222bcf361a8da6e38a06
    • 您的应用在沙盒化时使用一组单独的目录。如果您希望访问用户的 ~/Documents 目录,请确保遵循以下答案:stackoverflow.com/a/59752583
    猜你喜欢
    • 2011-12-31
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2015-07-28
    • 2018-07-18
    • 2016-08-27
    • 1970-01-01
    • 2018-04-21
    相关资源
    最近更新 更多