NEW BOOK! Swift Gems: 100+ tips to take your Swift code to the next level. Learn more ...NEW BOOK! Swift Gems:100+ advanced Swift tips. Lear more...
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 X 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.

Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Check out our new book!

Swift Gems

100+ tips to take your Swift code to the next level

Swift Gems

100+ tips to take your Swift code to the next level

  • Advanced Swift techniques for experienced developers bypassing basic tutorials
  • Curated, actionable tips ready for immediate integration into any Swift project
  • Strategies to improve code quality, structure, and performance across all platforms
  • Practical Swift insights from years of development, applicable from iOS to server-side Swift