首页 > 解决方案 > Swiftui foreach 索引超出范围 ondelete 问题

问题描述

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

标签: swiftforeachswiftui

解决方案


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)
        })

推荐阅读