How to Find the Index of an Element in a Swift Array? Step-by-Step Tutorial

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why "Find Index" is a Fundamental Skill You Need to Know by Heart

\n\n

Imagine: you are writing an iOS app. You have an array of tasks, and the user wants to delete a specific task by its name. Or, perhaps, you are looking for the position of an element to update it in a list. In both cases, you need the same thing — to find the index of an element in a Swift array. This task is so popular that the question on Stack Overflow has gathered thousands of views. And this is no coincidence: finding an index is one of the most common scenarios in everyday development.

\n\n

In this tutorial, we will cover all the ways to find the index of an element in Swift: from the simple firstIndex(of:) to advanced searching with closures. You will learn how to work with arrays of strings, numbers, and custom objects. Let's go!

\n\n

1. Preparation: What is an Index in a Swift Array?

\n\n

Before jumping into the code, let's recall the basics. An array in Swift is an ordered collection of elements. Each element has its own index — an integer starting from 0. The first element is index 0, the second is 1, and so on.

\n\n
let fruits = ["🍎 Apple", "🍌 Banana", "🍒 Cherry"]\n// Indices:         0           1           2\n
\n\n

When we say "find the index of an element," we mean: "Return the position number where this element is located." In Swift, there are several elegant methods for this. Let's go through them in order.

\n\n

2. The Classic Way: firstIndex(of:) — The Simplest Path

\n\n

Starting with Swift 4.2, the firstIndex(of:) method became the standard way to find the index of an element. It returns an optional Int? — if the element is found, you get the index; if not — nil.

\n\n

Example with an Array of Strings

\n\n
let fruits = ["Apple", "Banana", "Cherry", "Date"]\n\nif let index = fruits.firstIndex(of: "Cherry") {\n    print("Index of 'Cherry': \\(index)") // Output: 2\n} else {\n    print("Element not found")\n}\n
\n\n

Note: firstIndex(of:) searches for the first occurrence. If the array has duplicates, it will only return the first index.

\n\n

Example with an Array of Numbers

\n\n
let numbers = [10, 20, 30, 40, 50]\n\nif let position = numbers.firstIndex(of: 30) {\n    print("The number 30 is at position \\(position)") // 2\n}\n
\n\n

Important: This method only works with types that conform to the Equatable protocol. All standard types (Int, String, Double, Bool) already conform to it.

\n\n

3. Searching by Condition: firstIndex(where:) — Flexibility Without Limits

\n\n

But what if you need to find an index not by an exact value, but by a condition? For example, find the first element greater than 25, or a string that starts with a specific letter. For this, there is the firstIndex(where:) method (previously index(where:)).

\n\n

Syntax with a Closure

\n\n
let scores = [15, 22, 37, 41, 19]\n\nif let index = scores.firstIndex(where: { $0 > 25 }) {\n    print("The first element greater than 25 is at index \\(index)") // 2 (value 37)\n}\n
\n\n

Searching by an Object's Property

\n\n

Suppose you have an array of custom structures:

\n\n
struct User {\n    let name: String\n    let age: Int\n}\n\nlet users = [\n    User(name: "Anna", age: 25),\n    User(name: "Boris", age: 30),\n    User(name: "Victor", age: 22)\n]\n\n// Find the index of the user with the name "Boris"\nif let index = users.firstIndex(where: { $0.name == "Boris" }) {\n    print("Boris is at index \\(index)") // 1\n}\n
\n\n

This approach is incredibly powerful. You can use any boolean condition: check multiple properties, use regular expressions, or even call functions inside the closure.

\n\n

4. Working with Duplicates: How to Find All Indices

\n\n

M

Blogs

Book Recommendations