This is a companion discussion topic for the original entry at https://www.raywenderlich.com/18176818-your-first-ios-and-swiftui-app-polishing-the-app/lessons/11
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/18176818-your-first-ios-and-swiftui-app-polishing-the-app/lessons/11
1 Like
Hi teacher,
I have a question about “binding” in
The question is: If there is @binding, why shouldn’t we use “$game.score” but “game.score”?
The original code as below
struct BottomView: View {
@Binding var game: Game
var body: some View {
HStack{
NumberView(tittle: "Score", text: String(game.score))
Spacer()
NumberView(tittle: "Round", text: String(game.round))
@leeningthebest Here’s one way to think of it. A binding is a reference to some data that is “owned” elsewhere. In the case of game
, it’s owned by ContentView
but BottomView
has access to it as a binding here.
If you have a binding to a variable like game
, you can still access its properties directly without $
, so game.score
is fine.
But if you need to pass that binding to another view (that expects a binding), you use the $
, for example BottomView(game: $game)
.
I hope that helps!
1 Like