How to get element index in ForEach in SwiftUI?
Hi everyone! I'm new to SwiftUI and encountered a problem while working with ForEach. I have an array of strings, and I want to display them as a list, but I need to know the index of each element to, for example, delete an element on button tap.
Here's my code:
```swift
struct ContentView: View {
@State private var items = ["Apple", "Banana", "Cherry", "Melon"]
var body: some View {
List {
ForEach(items, id: \.self) { item in
HStack {
Text(item)
Spacer()
Button("Delete") {
// How to get the index of item to delete it?
// items.remove(at: ???)
}
}
}
}
}
}