Swift Array map() method

In this tutorial, you are going to learn about the Swift Array map() method with some easy-to-understand examples.

In Swift, the map() method allows you to transform an array by applying the same operation to each array element using a specified transformation closure.

Below is a simple example of using this method to add 5 with each element of an array of integer type:

var myInts = [1, 2, 3, 4]

// add 5 to each Array element
var transformed = myInts.map({ $0 + 5})



[6, 7, 8, 9]

Syntax of map() method

Below is the given syntax of the Array map() method:


In the above syntax, the array is the object of the class Array.

The returned value of map()

The map() method returns a new array that contains the transformed elements. The new array returned by the method will have the same length as the original array.

Parameter of map()

  • transform: In the parameter of the map() method, transform is the body of the closure that refers to the type of transformation to be done. Here we specify the transformation rules.

Examples of using map() method

Now let’s see some examples of using the method.

Example 1: Multiply array elements by 10

In the example below, we are multiplying each element of the array and form a new array using the map() method:

var myInts = [1, 2, 3, 4]

// Multiply each array element by 10
var transformed = myInts.map({ $0 * 10})



[10, 20, 30, 40]

Example 2: Uppercase each element of an array of string

Below is given another example where we are transforming an array using map() method to uppercase all letters:

var cities = ["Bangkok", "London", "Singapore", "Tokyo"]

// Using map() and uppercased() to transform
var transformed = cities.map({ $0.uppercased() })




In the above code, we are using the Swift uppercased() with the map() method to convert array elements to uppercase.

I hope, from this post you have understood the map() method and how its works.