Posted on 7 Dec 2023 . 3 min read
In SwiftUI, the ForEach is used to iterate over a collection of data and create views dynamically based on that data. If you need the index of the current element while using ForEach, you can achieve this by using the following ways along with ForEach:
If you want to use a range of indices with a count in SwiftUI’s ForEach loop, you can create a Range and iterate through it. Here's an example:
import SwiftUI
struct ContentView: View {
let items = ["Milk", "Bread", "Cookies", "Tea"]
var body: some View {
VStack {
ForEach(0..<items.count, id: \.self) { index in
Text("\(index): \(items[index])")
}
}
}
}In this example:
This method allows you to iterate through a range of indices based on the count of your collection and access the elements within the ForEach loop in SwiftUI.
In SwiftUI, you can use the indices property of a collection to access the indices while iterating through the elements using ForEach. Here's an example of how you can use indices to get the index within a ForEach loop. Here’s an example:
import SwiftUI
struct ContentView: View {
let items = ["Milk", "Bread", "Cookies", "Tea"]
var body: some View {
VStack {
ForEach(items.indices, id: \.self) { index in
Text("\(index): \(items[index])")
}
}
}
}In this example:
This approach allows you to iterate through the indices of the collection and use those indices to access the elements within the ForEach loop in SwiftUI.
If you need the index of the current element while using ForEach, you can achieve this by using the enumerated() method along with ForEach. Here’s an example:
import SwiftUI
struct ContentView: View {
let items = ["Milk", "Bread", "Cookies", "Tea"]
var body: some View {
VStack {
ForEach(Array(items.enumerated()), id: \.1) { index, item in
Text("\(index): \(item)")
}
}
}
}In this example:
This way, you can access the index and item simultaneously within the ForEach loop in SwiftUI.
Don’t hesitate to contact me if you have any questions or queries. Follow me on twitter @gurjitpt for any updates.
Thanks!
Written By

Gurjit Singh
I’m Computer Science graduate and CompTIA Security+ certified SOC Analyst and Mobile Application Security Engineer with 10+ years of cross-platform development experience across iOS, Android, and web.
Discover articles by topics
SwiftUI Class Struct Networking XCode NSCache Enum Optionals Property Observers Closures Guard Reviews StoreKit App Store Algorithms Testing Operators Protocol Extensions Weak Unowned SwiftData WWDC23 GCD API Admob SwiftLint Lottie Foreach Objective-C UIKit NavigationSplitView
Static analysis of an iOS app using MobSF, identifying credential misuse, security misconfigurations, and privacy issues....
2026-05-29 . 4 min read MobSF SAST
Transitioning from software development into cybersecurity is one of the most natural career moves in tech today....
2026-05-26 . 4 min read Security Certificates
Swift 6.1, officially released in March 2025, continues the evolution of Apple's powerful and expressive programming language....
2025-08-12 . 3 min read Swift 6.1
In any programming language, working with strings is essential, and Swift is no different.Whether you are building iOS apps......
2024-10-17 . 3 min read String Concatenation
With the introduction of SwiftUI, Apple has provided developers with a modern way to build user interfaces across all Apple platforms....
2024-07-09 . 3 min read UIHostingController
In the realm of software development, memory management plays a crucial role in ensuring the efficient allocation and deallocation of memory...
2024-01-28 . 4 min read Swift Autorelease