迁移到Swift 3之后的Spotlight索引问题

时间:2016-09-18 17:22:26

标签: ios swift3 spotlight

最近我将我的Xcode项目转换为Swift 3.0,现在我遇到Spotlight索引问题。我正在使用我的值的最终索引:

var searchableItems: [CSSearchableItem] = []
    let nameDays = ND.allnames()

    for (key, item) in nameDays as! [String:AnyObject] {
        let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String)
        let locItem = item["CZ"] as AnyObject
        if (locItem["long"] is String) {
            attributeSet.title = locItem["long"] as? String
        }
        else {
            attributeSet.title = locItem["basic"] as? String
        }
        let todayArr = key.characters.split{$0 == "-"}.map(String.init)
        let day = String(Int(todayArr[1])!) + "." + String(Int(todayArr[0])!) + "." as String
        attributeSet.contentDescription = day

        let searchableItem = CSSearchableItem(uniqueIdentifier: key, domainIdentifier: "namedays", attributeSet: attributeSet)
        searchableItems.append(searchableItem)
    }

CSSearchableIndex.default().indexSearchableItems(searchableItems) { (error) -> Void in
        if error == nil {
            print("Searchable items were indexed successfully")
        } else {
            print(error?.localizedDescription)
        }
}

但是当我运行应用程序时,Spotlight不会在我的应用程序中搜索任何内容。没有编译错误。

1 个答案:

答案 0 :(得分:0)

我能解决这个问题。 问题在于循环并从字典中分配。

let locItem = item.object(forKey: "CZ") as AnyObject!
        if (locItem?["long"] is String) {
            attributeSet.title = locItem?["long"] as? String
        }
        else {
            attributeSet.title = locItem?["basic"] as? String
        }