【发布时间】:2020-07-07 17:16:11
【问题描述】:
我正在努力了解 SwiftUI 绑定。在这里,我在视图中显示一个数组并将值绑定到第二个视图。在第二个视图中,我从数组中删除数据。
但是我得到以下信息,
致命错误:索引超出范围
我没有收到self.person.notes.remove(at: self.index) 的错误,实际上这实际上是在删除数组中的注释。使用ForEach 时,它必须在第一个视图中,因为数组已被修改,现在它超出了范围。但我不确定如何解决这个问题?当然Binding 应该已经解决了这个问题。
查看 1
ForEach(self.person.notes.indices, id:\.self) { index in
NoteView(person: self.$person, room: self.$home.notes[index], index: index)
}
查看 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")
}
}
知道这应该如何在 SwiftUI 中工作吗?
【问题讨论】:
-
stackoverflow.com/a/63145650/1652402 是适合我的解决方案