【发布时间】:2016-02-01 21:28:59
【问题描述】:
所以看起来 Swift (2.x) 字典上的过滤器函数返回一个元组数组。我的问题是有一个优雅的解决方案可以把它变成字典吗?提前致谢。
let dictionary: [String: String] = [
"key1": "value1",
"key2": "value2",
"key3": "value3"
]
let newTupleArray: [(String, String)] = dictionary.filter { (tuple: (key: String, value: String)) -> Bool in
return tuple.key != "key2"
}
let newDictionary: [String: String] = Dictionary(dictionaryLiteral: newTupleArray) // Error: cannot convert value of type '[(String, String)]' to expected argument type '[(_, _)]'
【问题讨论】:
标签: dictionary filter swift2 tuples