Group Group Group Group Group Group Group Group Group

Ch 9 - Cannot render completed example

I’m on the last page of chapter 9 before the “cleaning up” section. It states that I need to change ScoreView's initializer from numberOfAnswered to $numberOfAnswered in order to utilize the new change from @State to @Binding. Then it states:

You need to repeat that same change in ScoreView’s preview. Once that’s done, try ChallengeView using live preview. When you tap now, both counters update

However, when I add this code following that example:

struct ScoreView: View {

    let numberOfQuestions: Int
    @Binding var numberOfAnswered: Int

    var body: some View {
        HStack {
            Text("\(numberOfAnswered)/\(numberOfQuestions)")
                .font(.caption)
                .padding(4)
            Text("ScoreView Counter: \(numberOfAnswered)")
            Spacer()
        }
    }
}

struct ScoreView_Previews: PreviewProvider {

   @State static var numberOfAnswered: Int = 0

    static var previews: some View {
        ScoreView(
            numberOfQuestions: 5,
            numberOfAnswered: $numberOfAnswered // <----- Throws Error
        )
    }
}

Before even trying to render the ChallengeView, it throws an error in ScoreView's preview stating:

cannot convert value ‘$numberOfAnswered’ of type ‘Binding’ to expected type ‘Int’, use wrapped value instead

What am I doing wrong?

Hi,

sorry if I took so long to reply… I tried your code “as is” and it compiles and previews fine here. Have you been able to figure out what’s the problem?