How can I loop from last item to first in a SwiftUi view?
I’ve tried the following
let myarray = ["1", "2", "3", "4"]
ForEach(stride(from: myarray.count, to: 1, by: -1), id: \.self) { i in
print(myarray[i])
}
//desired output: 4 3 2 1
Thanks!!
>Solution :
You can use reversed:
ForEach(myarray.reversed(), id: \.self) { item in
Text("\(item)")
}
Note that I’m using Text, not print — this is SwiftUI, so you should have View code inside a ForEach.