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.

As someone who has worked extensively with Swift, I've gathered many such insights over the years. I'm excited to share these in my new book Swift Gems. This book is packed with advanced tips and techniques to help intermediate and advanced Swift developers enhance their coding skills. From optimizing collections and handling strings to mastering asynchronous programming and debugging, "Swift Gems" provides practical advice to elevate your Swift development. Grab your copy of Swift Gems and let's explore the advanced techniques together.

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

Level up your Swift skills!$35

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

Swift Gemsby Natalia Panferova

  • 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

Level up your Swift skills!

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

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

Swift Gems

by Natalia Panferova

$35