Greetings, can’t increase number of times ForEach is repeated, any solution?
import SwiftUI
struct ContentView: View {
@State var numberOfTimes: Int = 0
var body: some View {
VStack {
ForEach(1..<numberOfTimes, id: \.self) { i in
Text("i \(i)")
}
Text("Add one")
.onTapGesture {
numberOfTimes = numberOfTimes + 1
}
}
}
}
You should explain what "can't increase" mean. Do you get a crash ?
This would be logic.
- At start, numberOfTimes = 0
- range 1..<numberOfTimes is empty and it crashes: Fatal error: Range requires lowerBound <= upperBound
You should change to:
@State var numberOfTimes: Int = 1
You can make an additional change:
ForEach(1...numberOfTimes, id: \.self) { i in
Note: if works, don't forget to close the thread by marking the correct answer. The same for the other questions you posted.