WWDC23 deal: 40% off our book "Integrating SwiftUI into UIKit apps"! Learn more ...WWDC23 deal:40% off "Integrating SwiftUI into UIKit apps" >>
Quick Tip Icon
Quick Tip

Iterate over items and indices in Swift collections

I used enumerated() method before without giving it too much thought when I needed to get items and indices in Swift collections. But it's actually not the right way to do it.

The integer value we get with enumerated() shouldn’t be used as index into the collection unless the collection is zero-based and integer-indexed. It's a counter that always starts from zero.

var ingredients = ["potatoes", "cheese", "cream"]

for (i, ingredient) in ingredients.enumerated() {
    // do something with the counter number
    print("ingredient number \(i + 1) is \(ingredient)")
}

To iterate over items and indices we can use zip() function instead.

// Array<String>
var ingredients = ["potatoes", "cheese", "cream"]

// Array<String>.SubSequence
var doubleIngredients = ingredients.dropFirst()

for (i, ingredient) in zip(
    doubleIngredients.indices, doubleIngredients
) {
    // do something with the index
    doubleIngredients[i] = "\(ingredient) x 2"
}

We can even avoid using enumerated() entirely and count items with zip() function as well. This way we can control better whether we start counting from 0 or from 1.

var ingredients = ["potatoes", "cheese", "cream"]

for (i, ingredient) in zip(1..., ingredients) {
    // do something with the counter number
    print("ingredient number \(i) is \(ingredient)")
}

For a better interface over zip() we can also use indexed() method from Swift Algorithms. It's equivalent to zip(doubleIngredients.indices, doubleIngredients) but might be more clear.

import Algorithms

// Array<String>
var ingredients = ["potatoes", "cheese", "cream"]

// Array<String>.SubSequence
var doubleIngredients = ingredients.dropFirst()

for (i, ingredient) in doubleIngredients.indexed() {
    // do something with the index
    doubleIngredients[i] = "\(ingredient) x 2"
}

These examples come from a Twitter thread that I started but that collected many interesting tips and discussions from different people in the end. You can give it a read if you like.

Integrating SwiftUI into UIKit Apps by Natalia Panferova book coverIntegrating SwiftUI into UIKit Apps by Natalia Panferova book cover
WWDC23 offer
Our book "Integrating SwiftUI into UIKit apps" is now 40% off!

Discover various ways to adopt SwiftUI in existing UIKit projects and take full advantage of the new iOS 16 frameworks and APIs such as Swift Charts.

The offer is active until the 19th of June.