.package(url: "https://github.com/mesqueeb/MapSugar", from: "0.1.4")
Swift 辅助工具,可以更轻松地映射诸如 .mapKeys
、.mapKeysAndValues
、.mapValuesUsingKeys
之类的操作
.mapKeys
let newDictionary = ["a": 1, "b": 2]
.mapKeys { $0.uppercased() }
print(newDictionary) // ["A": 1, "B": 2]
.mapValuesUsingKeys
let newDictionary = ["a": 1, "b": 2].mapValuesUsingKeys { value, key in
return "\(key)\(value)"
}
print(newDictionary) // ["a": "a1", "b": "b2"]
.mapKeysAndValues
let newDictionary = ["a": "x", "b": "y"].mapKeysAndValues { key, value in
return (key.uppercased(), value.uppercased())
}
print(newDictionary) // ["A": "X", "B": "Y"]
查看文档了解更多信息。