2

I want to search for the key id in the dictionary. I have a dictionary like this:

var tableData:[String:Any] = ["id":["path":"","type":"","parameters":[]]]

Table data has 307 items, and all id's unique. I want to search in dictionary key id, like when I write "get" , it need the show all the search results with "get" in table view.

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()
}

When I try to search a word it prints "no match", in the debug, unable to read data writes for the value of the entry. Thank you in advance!

slytrkmn
  • 272
  • 1
  • 4
  • 15

3 Answers3

3

In fact your keys have to be unique and as in your case id is a top level key you don't have to perform filtering in order to access its value. Simply use tableData[searchText] to grab its value.

If you don't know the id value and you want to loop through all the keys you can do so like

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

As per what you already have in place you need to do the following

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")
}

Or you can simply get a new filtered array from your data source like

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")
Pancho
  • 4,099
  • 1
  • 21
  • 32
1

To access an element in a Dictionary with a key, use the following code.

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

For further information, have a look at:

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

Vlad
  • 932
  • 6
  • 18
  • I have unique 307 id. And I don't know all names, I want to search in it, like when I write **get** , it need the show all the search results with **get** in table view. – slytrkmn Jun 09 '17 at 07:51
0

Try using first(where:) directly on tableData, like this:

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()
}
Matusalem Marques
  • 2,399
  • 2
  • 18
  • 28