I have a form to select some users and assign them a int value.
The model:
class ReadingTime: Identifiable, Hashable {
var id: Int
@State var user: User
@Published var value: Int
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
static func == (lhs: ReadingTime, rhs: ReadingTime) -> Bool {
lhs.id == rhs.id
}
init(id: Int, user: User, value: Int) {
self.id = id
self._user = State(wrappedValue: user)
self.value = value
}
}
The view:
@Binding var times: [ReadingTime]
@State var newUser: User?
func didSelect(_ user: User?) {
if let user = user {
readingTime.append(ReadingTime(id: readingTime.nextMaxId,
user: user,
value: 0))
}
}
// In the body:
VStack(alignment: .leading, spacing: 0) {
HStack {
Picker("Select a user", selection: $newUser.onChange(didSelect)) {
ForEach(users) {
Text($0.name).tag(Optional($0))
}
}
.id(users)
}
VStack(spacing: 8) {
ForEach(0..<times.count, id: \.self) { i in
HStack(spacing: 0) {
Text(times[i].user.name)
TextField("ms", value: $times[i].value, formatter: NumberFormatter())
Button(action: {
NSApp.keyWindow?.makeFirstResponder(nil)
if let index = times.firstIndex(where: { $0 == times[i] }) {
times.remove(at: index)
}
newUser = nil
}, label: {
Text("REMOVE")
})
}
}
}
}
}
}
It looks like this:
However, when deleting an entry in the list, I get this error:
Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift
What's going on here?