|
I'm running into a problem I can't explain where modifiying elements of a list after deletions leads to a crash. First here's an example which doesn't crash to illustrate the setup:
Here, tapping on a row causes the corresponding quantity to increase, and in particular this continue to work without any crashes even after deleting some elements of the list. Now, suppose that instead of using a
At first glance all seems fine, but if I first delete the second entry ('Oranges') of the list and then submit something to the remaining 'Bananas' text field, the app crashes consistently. Naturally this is a simplified example, and there are some workarounds here such as using I'm running Xcode 14.3.1. Thanks in advance! |
|
hi, my guess is that when SwiftUI first instantiates the bananas view and puts it on screen, the TextField closure that references the bananas item (probably by index 2 because of the binding) is being captured ... it is that would explain the index out of range error you're getting. in contrast, an .onSubmit modifier is code that's executed in real time, not code that was previously captured and kept around for later use. so that works fine. you might look at using an .onChange(of: input) modifier attached to the TextField ... i did not test, however. hope that's sort of on target for you, DMG |
|
Try replacing the ForEach with this. (I'm still on MacOS 12 so I cannot test it.)
|
|
Thank you both for the insightful replies, with your help I've managed to fix the issue! |
SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.