0
    struct PocketListView: View {
    @EnvironmentObject var pocket:Pocket
    var body: some View {
        NavigationView{
            List{
                ForEach(self.pocket.moneyList.indices,id: \.self){index in
                    NavigationLink(destination:  MoneyView(money: self.$pocket.moneyList[index])){
                        MoneyNoTouchView(money: self.$pocket.moneyList[index])
                    }


                }.onDelete(perform: {index in
                    self.pocket.moneyList.remove(at: index.first!)
                })
                Spacer()
                HStack{
                    Image(systemName: "plus")
                        .onTapGesture {
                            self.pocket.add()
                    }
                }

            }
        }

    }
}


    struct Money {
    var id = UUID()
    var value = 0
}

    class Pocket: ObservableObject,Identifiable {
        @Published var id = UUID()
        @Published var moneyList = [Money]()

        func add() {
            self.moneyList.append(Money())
            print(moneyList.count)
        }
    }

When I try to remove any row,the app will crash and i get this"Fatal error: Index out of range". If i remove the NavigationLink Part in my code,it will be OK to remove any row. How can i solve this? Thank you.

DarylPan
  • 33
  • 1
  • 4
  • See if either of these links help you: https://programmingwithswift.com/delete-list-item-with-swiftui/ https://www.hackingwithswift.com/quick-start/swiftui/how-to-let-users-delete-rows-from-a-list –  Mar 05 '20 at 14:34
  • show us your code for moneylist, pocket and indices – Chris Mar 05 '20 at 15:36
  • @dfd but this guide do not use index in foreach,how can i pass a Binding kind var to sub view – DarylPan Mar 05 '20 at 15:59
  • @Chris Code has been added – DarylPan Mar 05 '20 at 16:03

1 Answers1

1

thx for your code.

I had to change/extend a few things, because there is still missing some code to compile your example but my code works....maybe it helps you

import SwiftUI


struct A : View {

    var body: some View {
        Text("a")
    }
}

struct ContentView: View {
    @EnvironmentObject var pocket:Pocket
    var body: some View {
        NavigationView{
            List{
                ForEach(self.pocket.moneyList.indices,id: \.self) { index in
                    NavigationLink(destination: A()){
                        Text("\(self.pocket.moneyList[index].value)")
                    }


                }.onDelete(perform: {index in
                    self.pocket.moneyList.remove(at: index.first!)
                })
                Spacer()
                HStack{
                    Image(systemName: "plus")
                        .onTapGesture {
                            self.pocket.add()
                    }
                }
            }
        }
    }
}


struct Money {
    var id = UUID()
    var value = 0
}

class Pocket: ObservableObject,Identifiable {
    @Published var id = UUID()
    @Published var moneyList = [Money]()

    func add() {
        self.moneyList.append(Money())
        print(moneyList.count)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {

        var pocket = Pocket()
        pocket.moneyList.append(Money(id: UUID(), value: 1))
        pocket.moneyList.append(Money(id: UUID(), value: 2))
        pocket.moneyList.append(Money(id: UUID(), value: 3))

        return ContentView().environmentObject(Pocket())
    }
}
Chris
  • 7,579
  • 3
  • 18
  • 38
  • I find the difference.So in NavigationLink I cannot pass a Binding param to sub view or i will meet the out of index error when i try to remove a row.2 ways to avoid: 1.no subview just put code here 2.do not pass Binding – DarylPan Mar 05 '20 at 17:20