Loop over Swift array to get elements


In this Swift programming tutorial, you will learn how to loop over an array to get each item. This tutorial actually shows you how to loop through array items to access or get each element of the array. It will help programmers to get all the items of a Swift array.

Swift already comes with a set of ways that are specially built for looping through items of an array.

Using Swift for-in loop to get array elements

Using a Swift for-in loop or for loop is the fastest and easiest way to loop over an array. Below is the syntax for looping over an array using for loop:

for element in array {
    //Get each element
}

Examples:

See the example below:

var fruitsArray = ["Lemon", "Strawberry", "Banana", "Apple", "Avocado"]

for fruit in fruitsArray {
    print(fruit)
}

Output:

Lemon
Strawberry
Banana
Apple
Avocado

In the above example, we have used the for-in loop to print each item value from the Swift array of type string. In this example, the fruitsArray is the array and fruit defining each element of the array in the fo-in loop.

The example above is related to the string type array. Now let’s use the for-in loop to loop over an integer type array:

let myNumbers = [23, 12, 9, 17, 25]
for number in myNumbers {
    print(number)
}

The output of the above code will be:

23
12
9
17
25

Now see another example where we will add +1 in the value of each array item to increase value by 1:

let myNumbers = [1, 2, 3, 4, 5]
for number in myNumbers {
    var myNumber = number+1;
    print(myNumber)
    
}

Output:

2
3
4
5
6

In the above example, we have increased each array item value by 1 and printed it.

We can also use the forEach loop to print array items in Swift. The Swift forEach loop is specially built for looping over array elements. It is a widely used method in app development for listing items.

Below is the example:

var fruits = ["Lemon", "Strawberry", "Banana", "Apple", "Avocado"]

fruits.forEach { fruit in
    print(fruit)
}

After running the above piece of code we will see the output given below:

Lemon
Strawberry
Banana
Apple
Avocado

The code given below will work as same as you can see above but using the forEach loop in a different way:

var fruits = ["Lemon", "Strawberry", "Banana", "Apple", "Avocado"]

fruits.forEach {
    print($0)
}

In the above code, you can see that the fruit in has been omitted in the forEach loop which was previously used in the case of for loop. Here we have not used this explicit argument. But it still prints the same result. This is because the $0 is represents each element.

Also, read: Merge or concatenate two arrays in Swift

Now you may think if the output is the same as you can see using the for loop, then what is the difference.

Well, there is a difference between looping through an array using for loop and using a forEach loop. The loop can’t be exit part way without processing the rest of the available array items in case of the forEach loop.

Swift has another type of collection type of data which is dictionaries. I have included it in this tutorial because I think it is relevant as dictionaries also can be used and treated like an array.

A dictionary in Swift always has a key-value pair.

So let’s see the example below where you can see we loop over a dictionary item:

var personData = [
    "Age": "32",
    "Nationality": "American",
    "Gender": "Female",
    "Hometown": "Mancos, Colorado"
]

for person in personData {
    print("\(person.key): \(person.value)")
}

The output is:

Hometown: Mancos, Colorado
Gender: Female
Age: 32
Nationality: American

As you can see, we are printing each key and the corresponding value for items from the dictionary personData.

We can do the same using forEach loop:

personData.forEach {
    print("\($0.key): \($0.value)")
}

Or

personData.forEach { person in
    print("\(person.key): \(person.value)")
}

These will show the same output.