我有一個UITableView
和一個UISearchController
。我想根據UISearchBar.text
的NSPredicate
過濾我的數據數組(名爲allGames)。根據NSPredicate篩選結構數組SWIFT
我的代碼只能過濾字符串數組是這樣的:
func updateSearchResults(for searchController: UISearchController) {
filteredGames.removeAll(keepingCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (allGames).filtered(using: searchPredicate)
filteredGames = array as! [String]
self.tableView.reloadData()
}
這裏我過濾陣列。但是,如果我創建此結構:
struct Games {
var name: String?
var type: String?
var image: UIImage?
}
如何根據遊戲名稱和類型過濾數組?在此先感謝您的幫助。
[搜索欄Swift 3 - 不能在/包含運算符與集合中使用]的可能重複(https://stackoverflow.com/questions/41308007/search-bar-swift-3-cant-use-in- contains-operator-with-collection) - 不能將NSPredicate與結構數組一起使用。 –
另請參見[Swift - 使用UISearchController和NSPredicate使用結構數組搜索](https://stackoverflow.com/questions/32556328/swift-search-using-uisearchcontroller-and-nspredicate-with-an-array-of-結構體)或[在swift中搜索結構體](https://stackoverflow.com/questions/34360226/search-for-struct-in-swift)。 –
哦,我讀了很多問題,沒有找到這些問題! –