SwiftUI List、各行にToggle、Model側更新(1行分削除)、「Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift, line 444」
Example
List {
ForEach(self.eventsModel.nextEvents) { nextEvent in
HStack {
Text(nextEvent.calendar.title)
.foregroundColor(Color(nextEvent.calendar.cgColor))
Spacer()
Toggle(isOn: $eventsModel.nextEvents[nextEvent.index].isOn, label: {
Text("Label")
})
.labelsHidden()
}
}
}
eventsModel.nextEventsを更新。1行分削除。
上記コードのViewを表示。
「Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift, line 444」になる。
デバッグでStep実行するとならない。
Toggle部品以外で対応してみた
List {
ForEach(self.eventsModel.nextEvents) { nextEvent in
HStack {
Text(nextEvent.calendar.title)
.foregroundColor(Color(nextEvent.calendar.cgColor))
Spacer()
if nextEvent.isOn == true {
Image(systemName: "checkmark.square")
.onTapGesture {
print("\(eventsModel.nextEvents[nextEvent.index].calendar.title) off")
eventsModel.nextEvents[nextEvent.index].isOn = false
self.eventsModel.updateOffCalendar()
}
}
else {
Image(systemName: "square")
.onTapGesture {
print("\(eventsModel.nextEvents[nextEvent.index].calendar.title) on")
eventsModel.nextEvents[nextEvent.index].isOn = true
self.eventsModel.updateOffCalendar()
}
}
}
}
}




