Swiftui foreach 索引超出范围 ondelete 问题

时间:2021-04-24 00:10:56

标签: swift foreach swiftui

我遇到了这个问题,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 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

1 个答案:

答案 0 :(得分:1)

ForEach 将每个项目作为索引返回(来自 'index in'),您可以使用该项目而不是访问数组中的该项目。您只需要将资金数组本身传递给 ForEach。

我对复制部分有点困惑,但试试这个:

ForEach(funds, id: \.self) { index in
            //StandardRowView(word: index.name, number: index.balance)
            
            HStack{
                Text(index.name)
                Spacer()
                TextField("Amount", value: $index.balance, formatted: NumberFormatter.currency)
                    .keyboardType(.numbersAndPunctuation)
                    .multilineTextAlignment(.trailing)
                    .onChange(of: index.balance, perform: { value in
                        
                        //Resets the fund balance if it exceeds the savings balance
                        if value > savingsBalance || fundsTotal > savingsBalance{
                            index.balance = copy[index].balance // not sure here
                        }
                    })
            }
        }
        .onDelete(perform: { indexSet in
            funds.remove(atOffsets: indexSet)
        })