Swift array map() method: Tutorial & examples

Updated: April 19, 2023 By: Khue Post a comment

Overview

In Swift programming, the array map() method is used for transforming an array by applying the same operation to each element of the array using a specified transformation closure. The syntax of the map() method is:

array.map(transform)

Here, array is an object of the Array class, and transform is a closure body that takes an element of the array as its parameter and returns a transformed value of the same or of a different type.

In general, you will call the map() method like this:

array.map { (element) -> NewType in
    // code to transform element into NewType
}

If you feel these words are too boring and not-easy-to-understand, please see the following practical examples. They are arranged in order from basic to advanced, from simple to complex.

Examples

Example 1: Adding 1 to each element of an array of integers

The code:

// an array of integers
let numbers = [1, 2, 3, 4, 5]

// use map() to add 1 to each element
let transformed = numbers.map { $0 + 1 }
print(transformed)

Output:

2, 3, 4, 5, 6]

In this example, we use the map() method to add 1 to each element of the numbers array. We use a short-hand closure that takes each element as $0 and returns $0 + 1.

Example 2: Converting an array of strings to uppercase

In this example, we use the map() method to convert each element of an array of strings to uppercase. We use a short-hand closure that takes each element as $0 and calls the uppercased() method on it.

// an array of strings
let languages = ["swift", "sling academy", "python"] 

 // use map() to convert each element to uppercase
let transformed = languages.map { $0.uppercased() }

// print the result
print(transformed) 

Output:

["SWIFT", "SLING ACADEMY", "PYTHON"]

Example 3: Creating an array of squares from an array of integers

In this example, we use the map() method to create an array of squares from an array of integers. We use a short-hand closure that takes each element as $0 and returns $0 * $0.

let numbers: [Int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let transformed: [Int] = numbers.map { $0 * $0 } 
print(transformed)

Output:

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Example 4: Creating an array of custom objects from an array of dictionaries

In this example, we use the map() method to create an array of custom objects from an array of dictionaries. We define a struct called User that has two properties: name and age. We use a short-hand closure that takes each dictionary as $0 and returns a User object with the name and age values from the dictionary.

// define User struct
struct User {
    let name: String
    let age: Int
}

// an array of dictionaries
let users = [
    ["name": "Sekiro", "age": 33],
    ["name": "The Demon King", "age": 999],
    ["name": "The Legendary Wolf", "age": 93],
]

// use map() to create User objects from dictionaries
let transformed = users.map { dict -> User? in 
    if let name = dict["name"] as? String, let age = dict["age"] as? Int {
        return User(name: name, age: age)
    } else {
        return nil
    }
}

// print the result
print(transformed)

Output:

[Optional(main.User(name: "Sekiro", age: 33)), 
Optional(main.User(name: "The Demon King", age: 999)), Optional(main.User(name: "The Legendary Wolf", age: 93))]