【发布时间】:2017-11-11 02:50:24
【问题描述】:
我想在字典中搜索关键字 id。我有一本这样的字典:
var tableData:[String:Any] = ["id":["path":"","type":"","parameters":[]]]
表格数据有 307 项,并且所有 id 都是唯一的。我想在字典键 id 中搜索,就像我写 "get" 时,它需要用 "get" 显示所有搜索结果在表格视图中。
func updateSearchResults(for searchController: UISearchController) {
let searchString = searchController.searchBar.text
if let entry = tableData.keys.first(where: { $0.lowercased().contains(searchString) }) {
print(entry)
} else {
print("no match")
}
tableView.reloadData()
}
func didChangeSearchText(searchText: String) {
if let entry = tableData.keys.first(where: { $0.lowercased().contains(searchText) }) {
print(entry)
} else {
print("no match")
}
// Reload the tableview.
tableView.reloadData()
}
当我尝试搜索一个单词时,它会打印 “no match”,在调试中,无法读取 entry 值的数据写入。先感谢您!
【问题讨论】:
-
你想用 id 搜索?
-
举个合适的例子说明你想要什么
-
entry是一个数组吗? -
tableData.keys.first 是 "id" ,你要搜索什么?
-
我想在“id”中搜索。每个 id 都是唯一的。喜欢
[["a"::["path":"","type":"","parameters":[]]],["b"::["path":"","type":"","parameters":[]]],["c"::["path":"","type":"","parameters":[]]]]
标签: ios swift dictionary swift3