|
Hi there, I'm getting an index out of range when I drag up the last rendered token in a token stack. Setting the amount to 0. It seems that the foreach in PlayerTokensView don't get the updated token array count while rendering but cannot access the last element becouse it don't exists anymore... I don't understand. Can someone help me? Thanks!
|
|
The problem is that you are using this form of
According to Apple's docs, this version of If the data you are looping over with
|
|
Wow, thank you very much! I had come to the source of the problem but I couldn't find a solution... Thanks again |
SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Sorry, view works better now, it updates as soon as I add a token, but still get the above error when I delete a token.
I have updated all |
|
|
|
As you suggested I added the id: KeyPath and now code updates all necessary views. But still get the out of range error on delete. Maybe the error persists because I have to use OnDelete with indexsets? But I don't know how... I mean not calling it the usual way Thanks for your time
|
|
You are not protecting for an empty set.
Your code is
What happens when you have In this code, and / or possibly elsewhere you need to protect for an empty set for example consider
|
|
@Greenamberred, thanks for your reply, the error persists. And that because I think I never get an empty set because I always check and delete after. The error is raised in the view I think within the foreach, it isn't able to 'see' the modified array. |
|
But maybe your code solves another hidden bugs I didn't notice, think so I found a workaround by not deleting from the array but rendering only tokens wich have an amount > 0... It works but is a workaround anyway : / |
|
It's crashing because of this:
when you remove the last item in the I'm pretty sure it's because of the |
|
Thanks again, but that's not the case, because the view crashes even when I delete NOT the last element, in which case the array has at least one element. But yes, maybe I have to rewrite the data stack |
|
Hmm, interesting. Removing the last remaining item in the array is the only time I saw a crash. And I was able to cause the same crash in a simpler, stripped-down example too. Can you post your |
|
Here it is...
|
|
This post just solved my issue about the ForEach that works better with constants and do not update the array when removing items... Thanks :) |
|
A crash when deleting the last item is a known bug in MacOS 10.15 (Catalina) and iOS 13. I don’t know about later versions of MacOS or iOS 14. See: https://blog.apptekstudios.com/2020/05/quick-tip-avoid-crash-when-using-foreach-bindings-in-swiftui/ https://stackoverflow.com/questions/63079221/deleting-list-elements-from-swiftuis-list |
|
I don’t know about later versions of MacOS or iOS 14. See: https://blog.apptekstudios.com/2020/05/quick-tip-avoid-crash-when-using-foreach-bindings-in-swiftui/ https://stackoverflow.com/questions/63079221/deleting- purolator freight tracking |
SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.