I am trying to get my head around SwiftUI bindings. Here I display an array in a view and bind the values to the second view. In the second view I remove the data from the array.
However I get the following,
Fatal error: Index out of range
I am not getting an error for self.person.notes.remove(at: self.index)
in fact this is actually removing the note in the array. It must be in the first view when using ForEach
as the array has been modified and now it is out of bounds. But I am unsure how to get around this ? Surely the Binding
should have resolved this.
View 1
ForEach(self.person.notes.indices, id:\.self) { index in
NoteView(person: self.$person, room: self.$home.notes[index], index: index)
}
View 2
@Binding var person: Person
@Binding var note: Note
var index: Int
if self.index > 0 {
Button(action: {
self.person.notes.remove(at: self.index)
}) {
Text("Remove")
}
}
Any idea how this is supposed to work in SwiftUI ?