I'm trying to present the items of an array in multiline text by looping over them in a ForEach. I need them to be able to be deleted, reordered and edited.
I'm getting the index out of range error when I try to delete any of the items, and it's getting thrown in the AppDelegate (I tried stepping through to see where it was). If I replace the TextEditor with a regular text field I can delete without issue (TextField also crashes). It seems like the loop is throwing the error instead of updating the array?
I can pass in the text as a constant and it doesn't crash, but then I can't edit it.
Here is a simplified version of my code that I created to troubleshoot the issue.
struct ContentView: View {
@State private var editMode: EditMode = .active
@State var array = ["one", "two", "three", "four"]
var body: some View {
List{
ForEach (0..<array.count, id: \.self) { index in
//Text(array[index])
//TextField("", text: array[$0])
TextEditor(text: $array[index])
}
.onDelete { indexSet in
let removeIndex = Int(Array(indexSet).first!)
array.remove(at: removeIndex)
}
}
}
}
I created another View that takes in the unbound value and displays it in a TextEditor, but I feel like that's not a good solution. I am new to Swift, so maybe I'm missing something simple? Is this how I should be creating this screen to do what I want in SwiftUI? Thanks!
I'm getting the index out of range error when I try to delete any of the items, and it's getting thrown in the AppDelegate (I tried stepping through to see where it was). If I replace the TextEditor with a regular text field I can delete without issue (TextField also crashes). It seems like the loop is throwing the error instead of updating the array?
I can pass in the text as a constant and it doesn't crash, but then I can't edit it.
Here is a simplified version of my code that I created to troubleshoot the issue.
struct ContentView: View {
@State private var editMode: EditMode = .active
@State var array = ["one", "two", "three", "four"]
var body: some View {
List{
ForEach (0..<array.count, id: \.self) { index in
//Text(array[index])
//TextField("", text: array[$0])
TextEditor(text: $array[index])
}
.onDelete { indexSet in
let removeIndex = Int(Array(indexSet).first!)
array.remove(at: removeIndex)
}
}
}
}
I created another View that takes in the unbound value and displays it in a TextEditor, but I feel like that's not a good solution. I am new to Swift, so maybe I'm missing something simple? Is this how I should be creating this screen to do what I want in SwiftUI? Thanks!