Anonymous contributor's avatar
Anonymous contributor
Anonymous contributor's avatar
Anonymous contributor
Published Nov 22, 2022
Contribute to Docs

The .map() method returns a new array containing the transformed values from calling the passed-in closure on each element of the given collection. It can be used on types that follow Swift’s Sequence and Collection protocols such as arrays, sets, and dictionaries.

Note: The original container is not modified, only the new array will contain the modified values.




let name = ["Gaeun", "River", "Minkyeong", "Dani"]
let lowercaseNames = name.map { $0.lowercased() }
let letterCounts = name.map { $0.count }

In the example above, each element in the name array is lower-cased and placed into a new array named lowerCaseNames. Then, the name array is mapped over again and a new array is returned with the count of each element. This will output:

["gaeun", "river", "minkyeong", "dani"]
[5, 5, 9, 4]

All contributors

Looking to contribute?

Learn Swift on Codecademy