我最近想使用 Core Spotlight 为我的一个项目中的搜索提供支持。但是,每当我将 CSSearchableItem 添加到 SearchIndex 时,我都会在完成处理程序中收到错误,其描述如下:
The operation couldn’t be completed. (CSIndexErrorDomain error -1.)
根据Apple的参考,错误代码-1
指的是Unknown Error
,这并不完全有帮助。我将 CoreSpotlight 和 CoreServices 框架添加到我的应用程序中,我真的不知道我可能做错了什么。
我整理了一个最小的例子:
import Foundation
import CoreSpotlight
print("Start indexing...")
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeText as String)
attributeSet.title = "test element"
attributeSet.contentDescription = "This is a description."
attributeSet.keywords = ["test1", "test2", "test3"]
let item = CSSearchableItem(uniqueIdentifier: "123455", domainIdentifier: "TestDomain", attributeSet: attributeSet)
var ready = false
CSSearchableIndex.default().indexSearchableItems([item]) { (error) in
if error == nil {
print("Success")
} else {
print(error?.localizedDescription)
}
ready = true
}
//Wait for the block to finish
while (ready == false) {
sleep(1)
}
print("Finish indexing...")
我成功地从 WWDC17 编译了 Apple 的 Core Spotlight 示例项目,它实际上可以正常工作,没有错误。但是,我无法通过系统范围的 Spotlight 搜索获取索引项。
有谁知道可能会发生什么?顺便说一句,我正在运行最新的 High Sierra 版本。
[编辑]刚刚看到,其实还有人有这个问题。然而,问题还没有得到解答:使用 CoreSpotlight 时出错 https://stackoverflow.com/questions/46880821/error-while-using-corespotlight
[Edit2] 更新到 10.13.2 后,行为发生了变化。放在操场上的这段代码现在正在运行;但是,放入我的应用程序中的相同代码仍然会产生错误,但它确实包含更多信息。打印错误对象会导致:
Error Domain=CSIndexErrorDomain Code=-1003 "(null)"
UserInfo={NSUnderlyingError=0x60000105f6e0
{Error Domain=NSCocoaErrorDomain Code=4097
"Couldn’t communicate with a helper application."}}
正如它向我显示的那样,这显然是框架中的一个错误,或者您对此有何看法?