【问题标题】:searching keys in dictionary swift快速搜索字典中的键
【发布时间】: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


【解决方案1】:

要使用键访问字典中的元素,请使用以下代码。

if let entry = tableData[searchText] {
   print(entry)
}

欲了解更多信息,请查看:

how can i get key's value from dictionary in Swift?

【讨论】:

  • 我有唯一的 307 id。而且我不知道所有的名字,我想在里面搜索,比如当我写 get 时,它需要在表格视图中用 get 显示所有搜索结果。
【解决方案2】:

事实上,您的密钥必须是唯一的,并且在您的情况下,id 是顶级密钥,您无需执行过滤即可访问其值。只需使用tableData[searchText] 即可获取其价值。

如果你不知道id 的值,并且你想遍历所有的键,你可以这样做

for key in tableData.keys {
   print(key)
   let value = tableData[key]
   // or do whatever else you want with your key value
}

根据您已有的内容,您需要执行以下操作

var tableData:[String:Any] = ["hello world":["path":"fgh","type":"dfghgfh","parameters":[]], "something else":["path":"sdfsdfsdf","type":"dfghfghfg","parameters":[]]]

if let entry = tableData.keys.first(where: { $0.lowercased().contains("hello") }) {
    print(entry)
    // prints 'hello world'
} else {
    print("no match")
}

或者您可以简单地从您的数据源中获取一个新的过滤数组,例如

let result = tableData.filter { (key, value) in
    key.lowercased().contains("else") // your search text replaces 'else'
}
/*
 * result would be an array with the objects based on your id search 
 * so you'll not only have the keys but the entire object as well
 */
print("Found \(result.count) matches")

【讨论】:

    【解决方案3】:

    尝试在tableData 上直接使用first(where:),如下所示:

    func updateSearchResults(for searchController: UISearchController) {
        guard let searchString = searchController.searchBar.text else { return }
    
        if let (id, entry) = tableData.first(where: { (key, value) -> Bool in key.lowercased().contains(searchString) }) {
            print(entry)
        } else {
            print("no match")
        }
        tableView.reloadData()
    }
    
    
    func didChangeSearchText(searchText: String) {
    
        if let (id, entry) = tableData.first(where: { (key, value) -> Bool in key.lowercased().contains(searchText) }) {
            print(entry)
        } else {
            print("no match")
        }
        // Reload the tableview.
        tableView.reloadData()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-05
      • 1970-01-01
      • 2014-09-19
      • 2013-07-14
      • 1970-01-01
      • 2013-11-29
      • 2018-02-03
      相关资源
      最近更新 更多