在以下代码中滑动删除SubView1
中的一行时出现错误。
class Model: ObservableObject {
@Published var items: [Item]
init(name: String, items: [Item]) {
self.items = items
}
func delete(at offsets: IndexSet) {
self.items.remove(atOffsets: offsets)
}
}
class Item: Identifiable, ObservableObject, Equatable {
static func == (lhs: Item, rhs: Item) -> Bool {
return lhs.id == rhs.id
}
var id = UUID()
var itemName: String
@Published var subItems: [SubItem]
init(itemName: String, payments: [SubItem]) {
self.itemName = itemName
self.subItems = payments
}
func delete(at offsets: IndexSet) {
self.subItems.remove(atOffsets: offsets)
}
}
class SubItem: Identifiable, ObservableObject, Equatable {
static func == (lhs: SubItem, rhs: SubItem) -> Bool {
return lhs.id == rhs.id
}
var id = UUID()
var isOn: Bool
init(isOn: Bool) {
self.isOn = isOn
}
}
struct MainView: View {
@EnvironmentObject var model: Model
var body: some View {
NavigationView {
List {
ForEach(model.items) {item in
NavigationLink(destination: SubView1(item: self.$model.items[self.model.items.firstIndex(of: item) ?? 0])) {
HStack{
Text(item.itemName)
ForEach(item.subItems) {subItem in
Text(subItem.isOn ? "True": "False")
}
}
}
}.onDelete(perform: model.delete)
}
}
}
}
// Here ------>
struct SubView1: View {
@Binding var item: Item
var body: some View {
List {
ForEach(0..<item.subItems.count) {index in
NavigationLink(destination: SubView2(subItem: self.$item.subItems[index])) {
ToggleView(subItem: self.$item.subItems[index])
}
}.onDelete(perform: item.delete)
}
}
}
struct SubView2: View {
@Binding var subItem: SubItem
var body: some View {
Toggle(isOn: $subItem.isOn) {
Text("Toggle-Text")
}
}
}
struct ToggleView: View {
@Binding var subItem: SubItem
var body: some View {
Toggle(isOn: $subItem.isOn) {
Text("Toggle-Text")
}
}
}
错误是:
Thread 1: Fatal error: Index out of range
我还尝试了SubView1
的另一个代码:
struct SubView1: View {
@Binding var item: Item
var body: some View {
List {
ForEach(item.subItems) {subItem in
NavigationLink(destination: SubView2(subItem: self.$item.subItems[self.item.subItems.firstIndex(of: subItem) ?? 0])) {
ToggleView(subItem: self.$item.subItems[self.item.subItems.firstIndex(of: subItem) ?? 0])
}
}.onDelete(perform: item.delete)
}
}
}
这是为什么?
答案 0 :(得分:-1)
可悲的是,您的代码无法编译,我不觉得自己在寻找错误。 ->提供一个编译并运行的示例,以便我们实际上可以尝试解决问题,而不必首先解决编译问题。 How to create a Minimal, Reproducible Example
通常,我建议您不要对带有空白索引的数组执行操作。特别是您知道的那些在运行时会被修改。 至少在执行操作之前对其进行测试。仅在数组为最终数组时才使用直接索引,否则进入索引超出范围的风险就太高了。
对于您的onDelete
,请尝试使用这种方法:
List {
ForEach(listItems, id: \.self) { (item) in
Text(item)
}.onDelete { (indexSet) in
self.listItems.remove(atOffsets: indexSet)
}
}
有关此内容的更多信息:Delete List item with SwiftUI
要对不安全的索引不执行操作,请尝试以下操作:
if let index = self.myData.firstIndex(of: self.item) {
self.myData.remove(at: index)
}
有关此内容的更多信息: Removing from array - Fatal error: Index out of range - SwiftUI Binding