【发布时间】:2021-04-24 00:10:56
【问题描述】:
我遇到了这个问题,onDelete 在一个视图中工作,但在我做同样的事情时它在不同的视图中不起作用。
在第一个视图中,它创建了一个空数组。
@State var funds: [Account.Fund] = []
当用户创建基金时,它会使用 ForEach 将它们打印到列表中。
if funds.count > 0{
ForEach(funds.indices, id: \.self) { index in
StandardRowView(word: funds[index].name, number: funds[index].balance)
}
.onDelete(perform: { indexSet in
funds.remove(atOffsets: indexSet)
})
}
这很好用。当我滑动并按删除时,它将删除而没有任何问题。但是,要创建基金,用户必须转到不同的视图。该“资金”变量被传递。
NavigationLink(
destination: AddFundView(funds: $funds, savingsBalance: $balance),
label: {
Text("Funds")
})
在 AddFundView 中它是一个 Binding。
@Binding var funds: [Account.Fund]
现在在 AddFundView 中,它会在用户添加它们时将它们打印到列表中。代码与之前类似,但确实有一些额外的内容,因此他们可以编辑输入的金额。
ForEach(funds.indices, id: \.self) { index in
//StandardRowView(word: funds[index].name, number: funds[index].balance)
HStack{
Text(funds[index].name)
Spacer()
TextField("Amount", value: $funds[index].balance, formatter: NumberFormatter.currency)
.keyboardType(.numbersAndPunctuation)
.multilineTextAlignment(.trailing)
.onChange(of: funds[index].balance, perform: { value in
//Resets the fund balance if it exceeds the savings balance
if value > savingsBalance || fundsTotal > savingsBalance{
funds[index].balance = copy[index].balance
}
})
}
}
.onDelete(perform: { indexSet in
funds.remove(atOffsets: indexSet)
})
当您在 AddFundView 中使用 onDelete 时,我收到此错误。
Swift/ContiguousArrayBuffer.swift:580: Fatal error: Index out of range
2021-04-23 20:06:56.400433-0400 WMMG[12180:9619685] Swift/ContiguousArrayBuffer.swift:580: Fatal error: Index out of range
我有点困惑,因为同样的行为也在应用程序的其他地方发生。我已经看到了一些关于此的先前问题和答案,并提到它与使用“索引”有关。但是,它在使用时有效,只是由于某种我不理解的原因并非每次都有效。谁能帮我解决这个问题?
更新: 它似乎与TextField有关。如果我评论它,它工作正常。我相信这与TextField的这一部分有关。我不确定如何获得我想要的行为。
value: $funds[index].balance
【问题讨论】: