SwiftUI
MVVM
iOS 15.0 RC
Xcode 13.0 (13A233)
Staus
Solution not found
Date
20 september 2021
I have come across some challenges with SwiftUI I can not get past. I am struggling with nested ForEach two-way bindings
Error
Index out of range?
Loses current position when a change is made
BruceLee.MP4
This error occurs in the second level as I choose to call it, that is, when an element is added to an array, which in turn is part of a structure that is an element in another array.
Structural description, hope this is understandable, yes ...
ForEach($parentData.children){$child in
ForEach($child.toys){$toy in
// Index out out of range -> when a new toy is added to toys
}
}
1. Select a parent
2. Edit parent
3. Select a child
4. Add a toy
5. Enter a name and tap Add button
The expected result when editing the toy, is that by pressing the done button you are brought to the presentation of the toy.
The current result is that you are brought all the way back to the parent. This has something to do with the fact that the list of published parents has been changed, so a redraw is called. It is able to show the correct parent, but it is not able to show the correct child / toy that was the reason for the redraw. Is this a SwiftUI feature or is it me implementing it wrong - that's what I'm wondering
To reproduce the situation, do the following:
2. Select a child
3. Select a toy
4. Tap Edit button
5. Change toy name
6. Tap Done button
This example has grown a little bigger than necessary as I have tried to solve the challenge in countless ways. Sorry