Chapters

Hide chapters

Swift Cookbook

Live Edition · Multiplatform · Swift · Editor agnostic

Use Array Methods in Swift
Written by Team Kodeco

Arrays in Swift are powerful data structures that can be manipulated in a variety of ways. In this cookbook entry, you’ll learn how to use some of the built-in methods of the Array type to perform common operations on your arrays.

append(_:)

One of the most basic operations you might want to perform on an array is to add new elements to it. The append(_:) method allows you to add a single element to the end of an array. Here’s an example of how to use it:

var numbers = [1, 2, 3]
numbers.append(4)
print(numbers) // [1, 2, 3, 4]

In this example, you start with an array of numbers [1, 2, 3]. You then use the append(_:) method to add the number 4 to the end of the array. The resulting array is [1, 2, 3, 4].

insert(_:at:)

Sometimes you may want to insert an element into an array at a specific position. The insert(_:at:) method allows you to do this. Here’s an example:

var numbers = [1, 2, 3]
numbers.insert(4, at: 1)
print(numbers) // [1, 4, 2, 3]

In this example, you start with an array of numbers [1, 2, 3]. You then use the insert(_:at:) method to insert the number 4 at index 1 of the array. The resulting array is [1, 4, 2, 3].

remove(at:)

You can also remove elements from an array using the remove(at:) method. This method takes the index of the element you want to remove as its argument. Here’s an example:

var numbers = [1, 2, 3]
numbers.remove(at: 1)
print(numbers) // [1, 3]

In this example, you start with an array of numbers [1, 2, 3]. You then use the remove(at:) method to remove the element at index 1 of the array, which is 2. The resulting array is [1, 3].

filter(_:)

The filter(_:) method allows you to create a new array containing only the elements that match a certain condition. This method takes a closure as its argument, which defines the condition for inclusion. Here’s an example:

var numbers = [1, 2, 3, 4, 5, 6]
let evenNumbers = numbers.filter { $0 % 2 == 0 }
print(evenNumbers) // [2, 4, 6]

In this example, you start with an array of numbers [1, 2, 3, 4, 5, 6]. You then use the filter(_:) method to create a new array containing only the elements that are even (i.e. those that satisfy the condition $0 % 2 == 0). The resulting array is [2, 4, 6].

map(_:)

The map(_:) method allows you to transform the elements of an array and create a new array from the results. This method takes a closure as its argument, which defines the transformation to be applied to each element. Here’s an example:

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

In this example, you start with an array of numbers [1, 2, 3, 4]. You then use the map(_:) method to create a new array containing the square of each element (i.e. applying the transformation $0 * $0). The resulting array is [1, 4, 9, 16].

reduce(::)

The reduce(_:_:) method allows you to combine the elements of an array into a single value. This method takes two arguments, an initial value and a closure that defines how to combine the elements. Here’s an example:

var numbers = [1, 2, 3, 4]
let sum = numbers.reduce(0, { $0 + $1 })
print(sum) // 10

In this example, you start with an array of numbers [1, 2, 3, 4]. You then use the reduce(_:_:) method to add the elements of the array together, starting with an initial value of 0. The resulting sum is 10.

© 2024 Kodeco Inc.