|
I am quite new to SwiftUI but allready bumping my head on the wall with a strange issue. I am displaying a list of items in a NavigationSplitView. And I want to update the content of my items in the associated detailed view. My datamodel is quite simple :
I am using 2 TextFields to display the Item content :
the updateItem function is simply updating my item in the items list. description is updating correctly but not quantity. I thought something was going wrong with the formatter. So I change the code to be :
The value from the TextField is updated correctly but item can't be updated with it.... Am I missing something here ? |
|
|
|
Your data model is a struct, which means the detail view gets a copy of it. You do not reveal how you modeled your list of items, nor the process for selecting an item within the first column of your list. We'll need more clues. In the meantime, you may want to check this excellent tutorial from Stewart Lynch on NavigationSplitViews. See -> NavigationSplitViews Please return here and tell us how you solved your issue. |
|
Ok let's add a little more detail. my list of items is a simple array, which is stored in a global Object for now. At first I passed the item directely as a binding to the detailed view. Since it was not working I get back to the Item Id to handle a fresh and independant copy in the detailView.
In ItemView, a fresh copy of the Item is first retrieved from the same global db Objects based on its id. So far everything is good. The updateItem() function is supposed to update the itemList with the new value. But the problem is that the item object is not holding the right value properties when it is called.
What I clearly not understand here is why the description property is updated correctly, but not the quantity property. The only difference is that one is a String while the other is an Int. |
|
I might experiment with:
|
|
This doesn't help either : "Cannot assign through dynamic lookup property: '$item' is immutable" |
|
I finaly found a workaround. Instead of updating item properties value, I simply instantiated a new Item to replace the initial one on updates. It works well. Looks like the additional overhead of formating text had some side effects when updating state variable values. |
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.