Skip to content

Instantly share code, notes, and snippets.

@MathVasc
Created February 9, 2018 20:19
Show Gist options
  • Save MathVasc/17881b7fba53d8fe10ac6d5276c81a76 to your computer and use it in GitHub Desktop.
Save MathVasc/17881b7fba53d8fe10ac6d5276c81a76 to your computer and use it in GitHub Desktop.
public static func setSpotLightIndexes(){
let items = DAOItem.sharedInstance.fetchItems()
var searchableItems = [CSSearchableItem]()
// deletando todos os itens
CSSearchableIndex.default().deleteAllSearchableItems { (error) in
guard error == nil else {
print(error!.localizedDescription)
return
}
}
// criando todos os itens
for item in items {
let attributeSet = CSSearchableItemAttributeSet(itemContentType: kUTTypeItem as String)
attributeSet.keywords = ["Groceries", "Grocery", "List", "Ericow"]
attributeSet.title = item.name
attributeSet.contentDescription = item.isDone ? "\(item.name!) was bought" : "\(item.name!) wasn't bought"
attributeSet.thumbnailData = item.isDone ? UIImagePNGRepresentation(UIImage(named: "Check")!) : UIImagePNGRepresentation(UIImage(named:"Uncheck")!)
searchableItems.append(CSSearchableItem(uniqueIdentifier: item.name!, domainIdentifier: nil, attributeSet: attributeSet))
}
// adicionando todos os itens
CSSearchableIndex.default().indexSearchableItems(searchableItems) { (error) in
guard error == nil else {
print(error!.localizedDescription)
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment