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.