2015-12-27 104 views
0

我在我的應用程序大辭典看起來像這樣:如何過濾詞典[字符串:字符串]]

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

,我想使用此功能func updateSearchResultsForSearchController(searchController: UISearchController)

UISearchController排序這個功能裏面了,我想不使用forfilter(includeElement: (Self.Generator.Element) throws -> Bool)過濾通過的「類型」我的數組,但我recive錯誤Cannot convert value of type String -> Bool to expected argument type ([String:String]) -> Bool

我做這樣的事情:

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

我應該怎麼做纔對?

回答

1

data有這種類型[[String:String]]。換句話說就是Array of [String:String]

因此,您傳遞給filter的閉合參數必須具有此類型:[String: String]

您可以使用以下代碼並用您的邏輯替換簡單的return true

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

請記住,dict是數組data第i個元素。

+1

你可以這樣簡化:'self.data.filter {$ 0 [「type」] ==「yourFilter」}' – ebluehands

+0

絕對是真的!在我的代碼中,我使用了「擴展版本」來明確封閉的參數是'[String:String]'。但是,謝謝你指出! –

+0

@ebluehands你能解釋我是什麼意思'{$ 0 [「type」] ==「yourFilter」}'特別是「$ 0」 – PiterPan

相關問題