PiterPan
PiterPan

Reputation: 1802

How can I filter dictionary [[String:String]]

I have a big dictionary in my App looks like that:

var data = [["type":"Sport", "model":"R6"],["type":"Enduro", "model":"Tenerre"],["type":"Chopper", "model":"Intruder"]]

which I would like to sort in UISearchController using this function func updateSearchResultsForSearchController(searchController: UISearchController)

inside this function up I would like to filter my array by "type" not using for but filter(includeElement: (Self.Generator.Element) throws -> Bool) but I recive an error Cannot convert value of type String -> Bool to expected argument type ([String:String]) -> Bool

I am doing something like this:

self.data.filter({ (type: String) -> Bool in
        return true     
    })

how should I do it correct?

Upvotes: 0

Views: 668

Answers (1)

Luca Angeletti
Luca Angeletti

Reputation: 59496

data has this type [[String:String]]. In other words is an Array of [String:String].

So the parameter of the closure you pass to filter must have this type: [String: String].

You can use the following code and replacing my simple return true with your logic.

let filteredData = self.data.filter { (dict:[String:String]) -> Bool in
    return true
}

Please keep in mind that dict is the i-th element of the Array data.

Upvotes: 1

Related Questions