my problem is: I have simple array with some Items. I want to display a List
with these items using a ForEach
with .indices()
.
(This is because my actual problem handles with Toggle
in a List
and for the isOn
binding I need the index to address a model that is bound to an EnvironmentObject
). So the solution to loop over the array items
is no possible solution for my problem.
The simplified starting point looks like this:
struct ContentView: View {
@State var items = ["Item1", "Item2", "Item3"]
var body: some View {
List {
ForEach(items.indices) {index in
Text(self.items[index])
}.onDelete(perform: deleteItem)
}
}
func deleteItem(indexSet: IndexSet) {
self.items.remove(atOffsets: indexSet)
}
}
If I now try to swipe-delete a row, I get this error message:
Thread 1: Fatal error: Index out of range
Debugging the index
value inside the closure, I can see, that the indices of the items
-array does not update. For example: If I delete the first row with "Item 1"
and inspect the value of index
after deleting the row it returns 2
instead of 0
(which is the expected first index of the array). Why is this and how can I fix this problem?
Thanks for your help!
Model
-class that has an array of[Item]
. AndItem
is a struct that conforms toIdentifiable
and has a Boolean-attribute. Now if I build the same View like above everything is fine. But when I replace theText()
-View with aToggle()
which is controlled by the Boolean-Value, I get another error on delete (this time inside theAppDelegate
):Thread 1: Fatal error: Index out of range
. What happens here? – Wow