Swiftly

Swift 5.7 references for busy coders

map

map is a functional method that returns an array that contains the results of mapping a given closure over the elements of a sequence. Similar methods are compactMap and flatMap.

map over array of Ints

let numbers = [1, 2, 3, 4]
let numbersSquared = numbers.map { $0 * $0 }
print(numbersSquared) // [1, 4, 9, 16]

map over array of Strings

let players = ["Ava", "Beatrix", "Chris"]
let lowercased = players.map { $0.lowercased() }
print(lowercased) // ["ava", "beatrix", "chris"]