fAiSaL
fAiSaL

Reputation: 774

Implementing search result for searching inside dictionary?

I have a dictionary like,

var DataDict:[String:[String]] = [String:[String]]()

DataDict[“fruits”] = [“apple”,”orange”,”pineapple”,”grape”]
DataDict[“vehicle”] = [“car”,”cycle / scooter“,”bike”]
DataDict[“colours”] = [“black”,”white”,”yellow”,”green”,”blue”]

so when I search in the search bar, if the searchText is fruits then the tableview should display the full array of fruits, or else whatever the searchText matches to the single object inside each key of DataDict?

So how will I achieve that to display the tableview. I need to implement that in searchBar textDidChange delegate.

Finllay I need to the display the result as, DataDict object as title and It's respective key as Subtitle.

ex:

apple
fruits

pineapple
fruits

Upvotes: 1

Views: 119

Answers (5)

Pallavi Srikhakollu
Pallavi Srikhakollu

Reputation: 598

I know its kind of late , but wanted to post an improved answer with the use of flatMap and filter.

var DataDict:[String:[String]] = [String:[String]]()

DataDict["fruits"] = ["apple","orange","pineapple","grape"]
DataDict["vehicle"] = ["car","cycle/scooter","bike"]
DataDict["colours"] = ["black","white","yellow","green","blue"]

let searchText = "orange"

func search() {
    var resultsArray = [String]()
    if DataDict[searchText] != nil {
        resultsArray = DataDict[searchText] ?? []
    }else {
        resultsArray = DataDict.flatMap{$0.1}.filter{ $0 == searchText}
    }

    print(resultsArray)
}

With the use of flat map you do not need to iterate each array of string as it flattens your nested dict into one. For more info on flatMap https://medium.com/@abhimuralidharan/higher-order-functions-in-swift-filter-map-reduce-flatmap-1837646a63e8 Hope this helps.

Upvotes: 1

Anjali jariwala
Anjali jariwala

Reputation: 445

You may get array in this way also for faster response..

let arrTemp = Array(DataDict.keys).filter { $0.contains(searchBar.text!)}
print(DataDict[arrTemp[0]])

Hope it will work for you.. :)

Upvotes: 1

Jeetendra Kumar
Jeetendra Kumar

Reputation: 510

try below method for searchingtext. Result can be displayed to table 

func searchText(string:String) -> [String] {
        let text = string
        var DataDict:[String:[String]] = [String:[String]]()

        DataDict["fruits"] = ["apple","orange","pineapple","grape"]
        DataDict["vehicle"] = ["car","cycle / scooter","bike"]
        DataDict["colours"] = ["black","white","yellow","green","blue"]
        var searchedItems = [String]()
        for key in DataDict.keys {
            if text == key {
                if let items = DataDict[key]  {
                    searchedItems.removeAll()
                    searchedItems.append(contentsOf: items)
                }
                break;
            }
            else {
                if let items = DataDict[key]  {
                    let filterd = items.filter({ (x) -> Bool in
                        return x.lowercased().contains(text.lowercased())
                    })
                    if filterd.count > 0 {
                        searchedItems.append(contentsOf: filterd)
                    }
                }
            }
        }

        print("SearchedItems: \(searchedItems)")
        return searchedItems
    }

Upvotes: 1

Hitesh Sultaniya
Hitesh Sultaniya

Reputation: 939

You can do like this,

var DataDict:[String:[String]] = [String:[String]]()

DataDict["fruits"] = ["apple","orange","pineapple","grape"]
DataDict["vehicle"] = ["car","cycle / scooter","bike"]
DataDict["colours"] = ["black","white","yellow","green","blue"]

let filterarray = Array(DataDict.keys).filter { $0.contains("searchText")}

print("\(filterarray)")

for string in filterarray {

    print("\(DataDict[string]!)")
}

Now you can display the as your requirement with filterarray

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100503

Create Empty array and initialize it will first key of the DataDict and make it the dataSource of the tableView

then every search replace it's contents with the new one that matches the search and reload the tableView

Upvotes: 0

Related Questions