I'm new to Swift / SwiftUI and trying to store the textfield value in an array during a foreach. However I'm encountering a Thread 1: Fatal error: Index out of range message. Should the storing the value happen outside the view(View Model) or can it be done inside a View??
struct OrderView: View {
@Binding var groupsize: Int
@State var nameArray = [String]()
var body: some View {
VStack {
ForEach(0 ..< groupsize) { index in
TextField("Insert name", text: $nameArray[index])}
}
}
}
groupsizewithnameArray.count?groupsizevariable that comes from the previous view.groupsize>nameArray.countwhich is the problem hereForEach(0 ..< groupsize - 1), but as @George_E mentioned, this might not be a good design choice