I am trying to code a leaderboard in SwiftUI. The data comes from an array of ranked users so that I should be able to get the rank from the index of the array, but I'm finding it surprisingly difficult. There are a lot of suggested approaches here but I can't get any to work.
I want each row to look like Rank | Name | Score eg:
1 Bob 443 2 Joe 411 3 Kevin 333
The data looks like:
var users: [[String]] = [["bob", "443"], ["joe", "411"], ["kevin", "333"]]
My code looks like:
ForEach(users, id: \.self) { user in
HStack {
Text("1")
Text(user.name)
Text(user.score)
}
}
How can I replace the 1 in the above with a counter variable?
mapoperation to add the rank as a property[[String]]is an array of an array of strings not an array of something that would have a name or a score. Also, what isn't working from all the solutions? Watch Demystify SwiftUI. Array indices in Swift start with zeroforEachis a loop,ForEachis aViewid: \.selfwon't work