I'm using ForEach to create a loop. However, I want to render a Rectangle conditionally depending upon the count of the loop. For example, don't render the rectangle if the last loop iteration.
What's the correct syntax for this?
I'm looking for something like this (non working pseudo-code)
ForEach(arrayOfThings, id: \.title) { thing in
// Stuff that happens on every iteration of loop
// Conditional section
if (thing.endIndex-1) {
Rectangle()
.frame(width: 100, height: 1, alignment: .bottom)
.foregroundColor(Color("666666"))
}
}
I am aware there are things like for (offset, element) in array.enumerated() { } but you can't use them in views. I wondered if there is a convenience feature in ForEach to solve this need?
Currently I am doing this to workaround:
ForEach(0..<arrayOfThings.count) { i in
// Stuff that happens on every iteration of loop
// Conditional section
if (i = self.arrayOfThings.count-1) {
Rectangle()
.frame(width: 100, height: 1, alignment: .bottom)
.foregroundColor(Color("666666"))
}
}