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

Check out our book!

Integrating SwiftUI into UIKit Apps

Integrating SwiftUI intoUIKit Apps

A detailed guide on gradually adopting SwiftUI in UIKit projects.

  • Discover various ways to add SwiftUI views to existing UIKit projects
  • Use Xcode previews when designing and building UI
  • Update your UIKit apps with iOS 16 features such as Swift Charts and Lock Screen widgets
  • Migrate larger parts of your apps to SwiftUI while reusing views and controllers built in UIKit