I have list of Account
s that each of them has a detail page and they are connected through @Binding
in the AccountDetailView
. Current code work well, updates are fine. No problem at all. However when I added the onDelete
modifier to the ForEach
below and tried swipe to delete
gesture in app, it crashes and says Fatal Error: Index out of range
twice.
I made some search and learned that ForEach
somehow does not get notified -or ignores it, idk much detail- and looks for the last index of the array. In the end, it can not find it and throws the error.
List {
Section(header: Text(String.empty), footer: Text(Strings.SectionFooters.accountListFooter.value)) {
ForEach(self.accountsModel.accounts.indices, id:\.self) { idx in
NavigationLink(destination: AccountDetailView(account: self.$accountsModel.accounts[idx])) {
AccountRow(account: self.$accountsModel.accounts[idx])
}
}.onDelete { (set) in
self.accountsModel.accounts.remove(atOffsets: set)
}
}
}
With keeping id: \.self
parameter in place it throws the error at the AppDelegate
, when I try to remove the parameter, the app works fine but again onDelete
it throws the same error at NavigationLink
's row above.
Here is the AccountDetailView
struct AccountDetailView: View {
@EnvironmentObject var accountsModel: AccountsViewModel
@Binding var account: Account
@State var isEditing: Bool = false
var body: some View {
...
}
}
Finally Account
class conforms to Codable
, NSObject
, Indentifiable
and some other class. I did not want to give all the code just because did not want make the question complicated and hard to examine. If requested, I will provide any part of the code. Thanks in advance.