The Ultimate Guide to Loops in Swift
By: Manuel Schulze
Tags: Swift Programming, iOS App Development, Mobile, Programming, Swift

Iterating over sequences with the for-in-loop

Now let’s look at the solution I use the most when it comes to loops in Swift.

Therefore, let’s first understand what we do most of the time in programming with loops.

Why do we loop in the first place? Most of the time it’s because we want to do an operation over and over again on a certain number of elements that oftentimes come from external sources (e.g. user input or network request).

We could now get the number of elements and then use the index-based solution above. Let’s see that in code:

In the code snippet above, we have 3 animals. We iterate over them with an index and access the element at the index of the array and print it out with string interpolation.

However, that is still error-prone. Imagine, we would have accidentally used the following range:

1..<animals.count

That would cause us to not print dog to the console. Or how about that:

0...animals.count

That would even crash our app as we try to access an element at an index that is not available.

Sure, all of that is of less severity when it comes to printing out values. Anyway, the same applies to critical business logic like transfers from one bank account to another. It is a problem when you don’t apply a currency exchange rate on one of the transactions.

So let’s revisit the example above with a better approach.

That looks nicer! But how does it work? It actually iterates over the sequence (in this case an array) and creates a constant of those values for each iteration that we can then use inside the loop.

We don’t need to handle indices here. We can simply use the element.

How does that work with dictionaries? Here is the code.

You can see, that we use the same loop but this time, we can access the key of the animal with the key and the value with the value property.

Did you know: You can actually use emojis in Swift. You can even go that far that you use them as variable names, but I would recommend avoiding that. It makes it harder to access them.

I will omit Sets as they work the same as arrays from a loop perspective. You simply need to know, that sets have no duplicates. But be careful! They are also not ordered! That can have some unintended side effects.

But what if you still need the index of the elements? What if you want to print out something like 0: Dog, 1: Cat, 2: Fish? Or for any other reason, you also need to know the index of the element?

The for-in-loop is pretty versatile and got a solution for that, too! You can use the function enumerated on the array and access the index as well as the element.

The enumerated function will create a tuple with the structure (n, x) where n is the index of the element starting from zero and x is the element itself.

With that, we can get access to the index alongside the actual element and use both of these values in the same loop without the need to alter the index or access the original elements array.

Relevance: I’ve already mentioned that I prefer this loop in that variation by far over all other loop types. It’s very versatile and you can access the index as well as the element or both at the same time. Make sure that you get used to the syntax because you will see it over and over again.