2016-09-18 81 views
0

最近我將Xcode項目轉換爲Swift 3.0,現在我遇到了Spotlight索引問題。我用我的價值觀的那些最終索引:遷移到Swift 3之後的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) 
     } 
} 

但是當我運行的應用程序,焦點不搜索我的應用程序內的任何東西。沒有編譯錯誤。

回答

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 
     }