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.
@State var itemList: Array<Item> = db.getItems()
@State private var selectedItem:Item.ID? = nil
@State private var isUpdatingItem:Bool = false
var body: some View {
NavigationSplitView() {
List(itemList, id: \.id, selection: $selectedItem) { item in
NavigationLink(value: item) {
Text(item.description)
}
}
} detail: {
ItemView(selectedItem: $selectedItem, isUpdated: $isUpdatingItem)
}
}
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.
@Binding var selectedItem: Item.ID?
@Binding var isUpdated: Bool
@State private var item: Item = Item()
[...]
VStack {
TextField(text: $item.description) {
Text("Description: ")
}
.onSubmit(updateItem)
TextField(value: $nb, format: .number) {
Text("Quantity: ")
}
.onSubmit {
print(nb) // nb is correctly updated with the value in the TextField
item.quantity = nb // item is not modified !!!!!!
print(item.quantity) // item still contains the old quantity value
updateItem()
}
}
.onAppear(perform: loadData)
[...]
func loadData() {
item = db.getItem(id: selectedItem)
}
func updateItem() {
db.updateItem(id: selectedItem, withValue: item)
isUpdated.toggle()
}
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.