Group Group Group Group Group Group Group Group Group

Question regarding chapter 8.4

Is there a reason for using @Binding var numberOfAnswered: Int instead of let numberOfAnswered: Int? Due to the fact that ScoreView should not change the value of the source of truth, it shouldn’t contain the Binding property wrapper, is my understanding correct?

Hi Ionut!

Binding notifies ScoreView to redraw whenever numberOfAnswered changes.

You could lose the Binding and it still works, because when the state of truth changes the view will automatically be redrawn, in our case ChallengeView, and when the view is redrawn it will redraw the children as well, in our case ScoreView

Hi @ionutac, one of the magic things of SwiftUI is that the redraw process is optimized - it selects the UI components that are affected by a state change, and updates those components only.
Without the binding, ScoreView has no source of truth - just an immutable value that it uses to display the score.

If numberOfAnswered changes in ChallengeView, that doesn’t propagate to ScoreView, because it only has immutable content - so in no case ScoreView will redraw itself if numberOfAnswered is updated in ChallengeView

There are cases where ScoreView is updated anyway, for other reasons (such as ChallengeView being reinstantiated), but I wouldn’t rely on that, because it won’t work properly.

Note the difference: a view is not redrawn because its parent asks it, it is redrawn when the state it depends upon changes.

1 Like

Thanks for the explanation!