複雜的字典(關聯數組)調用過濾器我有此數組:在迅速
class Filter {
var key = ""
var value = ""
init(key: String, value: String) {
self.key = key
self.value = value
}
}
let arry = [
"a":[Filter(key:"city",value:"aachen"),Filter(key:"city",value:"augsburg")],
"b":[Filter(key:"city",value:"bremen"),Filter(key:"city",value:"berlin")]
]
,我想尋找奧格斯堡等輸出看起來像這樣從具有過濾功能的詞典中刪除:
let arry = [
"a":[Filter(key:"city",value:"aachen")],
"b":[Filter(key:"city",value:"bremen"),Filter(key:"city",value:"berlin")]
]
我與許多過濾器和地圖constelations嘗試過,但我總是得到這樣的結構,結果是:
let arry = [
["a":[Filter(key:"city",value:"aachen")]],
["b":[Filter(key:"city",value:"bremen"),Filter(key:"city",value:"berlin")]]
]
例如使用此篩選器:
arry.map({ key,values in
return [key:values.filter{$0.value != "augsburg"}]
})
這裏有什麼問題?我如何過濾和映射更復雜的對象?
首先,你的數組是字典,你爲什麼要這麼做複雜,你不覺得,而不是使用屬性鍵和值做類Filter,你需要使用第一個字符包含第一個字符後面的城市和第二個字符串數組,包含該對象的所有城市名稱。 –
相關:http://stackoverflow.com/questions/24116271/whats-the-cleanest-way-of-applying-map-to-a-dictionary-in-swift。 –