- Mastering macOS Programming
- Stuart Grimshaw
- 71字
- 2021-07-02 22:54:33
Map with dictionaries
We can also map over the (key, value) pairs of a dictionary:
let numbers = ["1": "one",
"2": "two",
"3": "three"]
let other = numbers.map({(key, val) in
(Int(key), val.uppercased())
})
This syntax takes a little getting used to; just remember, you need to create a tuple when reading the key and value of each dictionary element into the closure, and similarly return the new values as a tuple.