|
Hi! App crashes when removing a row. If I create a new habit, then remove - it's ok. (although strangly animated) But if I create a new habit, then edit something in DetailView() and, then, remove, app crushes. This is the 'error' I get - "Thread 1: Fatal error: Index out of range" on the line of - Text("(habits.items[index].description)") (description is highlighted red) Paul is talking about similar issue in lists. (day37 https://www.hackingwithswift.com/books/ios-swiftui/working-with-identifiable-items-in-swiftui) So ,that I should do the following
But! What I was looking for is - to pass index to the next view, so in the DetailView() I can edit the habit. I need a reference and not a copy of value. The same with a list, as there I want to do the edits in ContentView(). For ex., increment the completion times amount. How to fix the List to work properly?
|
|
Hi, I’m having the same issue - my code the same as @MikeMaus. I can add and delete habits from the list without any issues. However, as soon as I call the DetailView() passing the index, return to the list and delete habits so that the number of habits left in the list is less than the index of the last habit viewed in the DetailView(), the app crashes. Stepping through the code, it appears that after calling the onDelete modifier the DetailView() code is executed again before the list view is rebuild. Because the index value is now higher than the size of the array, the app crashes. Any idea why onDelete would result in the DetailView() code being executed? Any suggestions on how to fix this issue or alternative solutions would be much appreciated. Thanks |
|
DetailView does not need to receive the entire array of habits. Just pass the habit itself. That should sort the problem. You do so by simply declaring a |
|
Thanks @MarcusKay - I tried @Binding but get an error: Cannot convert value of type 'Habit' to expected argument type 'Binding<Habit>' Is it because the habbitArray is within a class? Here's my code:
|
|
Sorry... it is because I forgot the
This makes it a binding being passed. |
|
Thanks again, but this time I get "Cannot find $habit in scope"
|
|
I've reverted back to my original solution of pasing the index to the HabitView and stopped my app crashing by coding around the issue - I now check the index value before accesses the habbitArray in the HabitView. It’s a pretty inelegant solution but it’s the best I can do with my current level of knowledge. Hopefully as I learn more in the course I’ll be able to come back and update this post with a better solution, or someone else will. Trying to fix it has been a REALLY useful exercise because I understand better what was causing the problem, even if I can’t fix it. The onDelete modifier was changing the habitArray which is a @Published property and therefore triggered all views observing it to reload, even if they were not being displayed on screen. When the HabitView reloaded it still had the last value of index which was now out of range because items had been deleted from the array. Here's my workaround:
|
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.