Group Group Group Group Group Group Group Group Group

Chapter 7 Challenge - dollar sign vs no dollar sign

Why is WelcomeView passed in the history variable without a dollar sign but ExerciseView needs the dollar sign? Is it because ExerciseView is modifying the data while WelcomeView isn’t?

struct ContentView: View {
  @State private var history = HistoryStore()
  @State private var selectedTab = 9

  var body: some View {
    TabView(selection: $selectedTab) {
      WelcomeView(history: history, selectedTab: $selectedTab)
        .tag(9)
      ForEach(0 ..< Exercise.exercises.count) { index in
        ExerciseView(history: $history, selectedTab: $selectedTab, index: index)
          .tag(index)
      }
    }
    .tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
  }
}

Hi @komocode and welcome to the forums!

Yes, exactly.

@State lets us change the property within the structure where it’s defined.

When we pass the property to another structure, we consider whether we have to modify the data.

If we do, we pass the property as a binding, using @Binding in the receiving structure. We prefix the property with the dollar sign.

If we don’t need to modify the data, the receiving structure can just define the property with let. In which case we don’t prefix the passed property.

`

1 Like

also, review the diagram “HistoryStore view tree” in section 7.2, where you figure out what kind of access each view needs

1 Like