I have a ForEach that I use to display some info, sorted on it's date attribute:
My view looks like:
ForEach(records.sorted(by: < ), id: \.self) { record in Text("\(record.date") } .onDelete(perform: deleteRecord)
Record.swift
class Record : Comparable { var value: Float var date: Date init(value: Float = 0.0, date: Date = Date.now) { self.value = value self.date = date } static func < (lhs: Record, rhs: Record) -> Bool { return lhs.date < rhs.date }}
My problem is that whenever I delete an item, the wrong one is actually removed.
deleteRecord()
func deleteRecord(_ indexSet: IndexSet) { for index in indexSet { movement.records.remove(at: index) } }
I'v tried changing the deleteRecord()
method to something like:
func deleteRecord(at offsets: IndexSet) { movement.records.remove(atOffsets: offsets) }
but this did not solved my issue.