• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Binding didSet variable to update computed var

Resolved: Binding didSet variable to update computed var

0
By Isaac Tonny on 16/03/2023 Issue
Share
Facebook Twitter LinkedIn

In this post, we will see how to resolve Binding didSet variable to update computed var

Question:

I am creating a nutrition tracker where I need the nutrients to update when the amount of the food is changed. This is dictated by the nutrition in the food and the amount eaten (obviously). I am trying to have my computed var (nutrients) update when the amount changes but what I have so far just reinitializes the object’s amount back to 1 instead of what is set in the Binding textfield.
There is also a measurement selector that needs to update and set the nutrient values but for the sake of simplicity I have just included the amount field. Can someone tell me where my thinking is wrong?
Example Struct
Example binding in View:
Nutrients struct:
Food Struct:

Best Answer:

Your syntax is slightly off when you pass in the new nutrient values:
is evaluated as the current food.consumableProp.nutrients.magnesium value or 0 * amount (which of course is always 0). Adding parentheses should fix your issue:
This change makes it work on my end, but I did still have to change a few things to get it to compile so let me know if it doesn’t work for you.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com

binding swift swiftui
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: significance letter above bar graphic in wrong order

01/04/2023

Resolved: VBA – Applying border around the areas with value/text

01/04/2023

Resolved: How can I implement a function depending on picked up items?

01/04/2023

Comments are closed.

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.