How to use index in SwiftUI list

Issue #515

Use enumerated and id: \.element.name

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct CountriesView: View {
let countries: [Country]

var body: some View {
let withIndex = countries.enumerated().map({ $0 })

return List(withIndex, id: \.element.name) { index, country in
NavigationLink(
destination: CountryView(country: country),
label: {
VStack(alignment: .leading) {
Text(country.name)
.styleMultiline()
}
.paddingVertically()
}
)
}
}
}

Comments