What's the cleanest way of applying map() to a dictionary in Swift?


In Swift, we can use the map() method to the dictionary to apply a transformation to the values of the dictionary. This method returns a newly created object with the same keys as the original dictionary but with the values transformed by the mapping function.

Example 1: Transforming Values with a Closure

In the following example, we are performing the multiplication on each value in the dictionary using the mapValues() function. We pass a closure that takes an argument. In the resulting dictionary, you can see that each value has been doubled.

import Foundation
let inputDictionary = [1: 2, 3: 4, 5: 6, 7: 8, 9: 10]
let modifiedDictionary = inputDictionary.mapValues { $0 * 2 }
print("Input Dictionary: \(inputDictionary)")
print("Modified Dictionary: \(modifiedDictionary)")

Output

Input Dictionary: [5: 6, 9: 10, 3: 4, 7: 8, 1: 2]
Modified Dictionary: [5: 12, 9: 20, 3: 8, 7: 16, 1: 4]

Note that mapValues() returns a new dictionary, so the original dictionary is not modified. If you need to modify the original dictionary in place, you can use a for...in loop to iterate over the keys and values of the dictionary and update the values as needed.

Example 2: Transforming key-value Pairs with a Closure

Here's an example of using map() on a dictionary of strings to create a new array of uppercase key-value pairs −

In Swift, you can use the map() function to transform a dictionary into an array of key-value pairs that have been transformed according to a closure that you provide.

import Foundation
let inputDictionary = ["apple": "red", "banana": "yellow", "orange": "orange"]
let transformedArray = inputDictionary.map { (key, value) in
   return (key.uppercased(), value.uppercased())
}
print("Input Dictionary: \(inputDictionary)")
print("Transformed Array: \(transformedArray)")

Output

Input Dictionary: ["apple": "red", "banana": "yellow", "orange": "orange"]
Transformed Array: [("APPLE", "RED"), ("BANANA", "YELLOW"), ("ORANGE", "ORANGE")]

In the above example, we transform the key-value pair into a tuple. Each tuple contains a key and value in upper case. We access the key-value pair using the map() function and return the tuple with an uppercased pair.

You should note that the resulting array is not the same as the original dictionary. If you want to keep the order, you can use the sorted() function to sort the output array by the original keys.

Example 3: Transforming Keys with a Closure

In this example, we're using map() to transform the dictionary keys by converting them to uppercase. The resulting transformedKeys array will contain the transformed keys ["ORANGE", "BANANA", "APPLE"].

import Foundation
let inputDictionary = ["apple": "red", "banana": "yellow", "orange": "orange"]
let transformedKeys = inputDictionary.map { (key, value) in
   return key.uppercased()
}
print("Input Dictionary: \(inputDictionary)")
print("Transformed Keys: \(transformedKeys)")

Output

Input Dictionary: ["banana": "yellow", "orange": "orange", "apple": "red"]
Transformed Keys: ["BANANA", "ORANGE", "APPLE"]

Example 4: Transforming a Dictionary of Objects

In this example, we have a dictionary where the values are objects of a custom class Person. We're using map() to transform the dictionary into an array of strings, where each string contains the person's name and age. We're accessing the name and age properties of each Person object using the value parameter of the closure.

import Foundation
class Person {
   var name: String
   var age: Int
    
   init(name: String, age: Int) {
      self.name = name
      self.age = age
   }
}
let peopleDict = ["Alice": Person(name: "Alice", age: 30),
                  "Bob": Person(name: "Bob", age: 25),
                  "Charlie": Person(name: "Charlie", age: 35)]
let namesAndAges = peopleDict.map { (key, value) in
   return "\(value.name) (\(value.age))"
}
print("namesAndAges: \(namesAndAges)")

Output

namesAndAges: ["Alice (30)", "Charlie (35)", "Bob (25)"]

Example 5: Mapping a Dictionary with Optional Values

In this example, we have a dictionary where the values are optional integers. We're using the compactMapValues() method to remove any key-value pairs where the value is nil. This returns a new dictionary containing only the non-nil values.

import Foundation
let inputDictionary: [String: Int?] = ["apple": 3, "banana": nil, "orange": 2, "pear": 4, "kiwi": nil]
let nonNilValues = inputDictionary.compactMapValues { $0 }
print("Input Dictionary: \(inputDictionary)")
print("nonNilValues: \(nonNilValues)")

Output

Input Dictionary: ["apple": Optional(3), "kiwi": nil, "banana": nil, "orange": Optional(2), "pear": Optional(4)]
nonNilValues: ["orange": 2, "apple": 3, "pear": 4]

Conclusion

A powerful higher-order function in Swift called map() enables you to change groups of components in accordance with a closure you supply. The keys, values, or key-value combinations of a dictionary can be changed into a new collection using the map() function when it comes to dictionaries.

Dictionary transformations using map() frequently involve changing a dictionary's values to a different type, changing a dictionary's keys or values using a closure, and converting a dictionary into an array of tuples or another dictionary with the same or different keys and values.

Updated on: 04-May-2023

585 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements