Home > AI > IOS > Foundation >

map

Example (Apple Official): map applies in an array

let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let lowercaseNames = cast.map { $0.lowercased() }
print(lowercaseNames)
let letterCounts = cast.map { $0.count }
print(letterCounts)

Example: map also applies in an Int

let possibleNumber: Int? = Int("42")
let possibleSquare = possibleNumber.map { $0 * $0 }
print(possibleSquare)
// Prints "Optional(1764)"

let noNumber: Int? = nil
let noSquare = noNumber.map { $0 * $0 }
print(noSquare)

Leave a Reply