I'm still trying to get my head around Swift/SwiftUI, so apologies for what's likely a really simple question. Consider this simple SwiftUI file:
import SwiftUI
struct sampleStruct {
let entryID: Int // comes from the system
let name: String
}
struct TestView: View {
var listEntries = [sampleStruct(entryID: 301, name: "Pete"),
sampleStruct(entryID: 5003, name: "Taylor"),
sampleStruct(entryID: 13, name: "Suzie")]
var body: some View {
VStack{
ForEach(listEntries, id: \.entryID) { idx in
Text(idx.name)
}
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
TestView()
}
}
Here's my question: I'm looking for a simple way to add the position of the element in the array to the text field. I.e. instead of Pete, Taylor, Susie, I'd like to get:
1 Pete
2 Taylor
3 Suzie
I thought that this could do the trick:
...
VStack{
ForEach(listEntries, id: \.entryID) { idx in
Text( listEntries.firstIndex { $0.id == idx.entryID } )
Text(idx.name)
}
}
...
But, I'm getting the error "No exact matches in call to initializer".
Any help would be appreciated,
Philipp