Learn Courses My Dashboard

Failed to produce diagnostic

Hello,
I’m newbie and I try to follow the beginning course.
I’m on lesson 11 et when I declare var for replace score and cards, I have this error :

“Failed to produce diagnostic for expression; please file a bug report”

on the line :
var body: some View {

I don’t understand why…

I put my code here below…

"…

//
// ContentView.swift
// war-challenge
//
// Created by Arnaud SCHMITT on 04/05/2021.
//

import SwiftUI

struct ContentView: View {

@State private var playerCard:String = "card5"
@State private var cpuCard:String = "card9"
@State private var playerScore:Int = 0
@State private var cpuScore:Int = 0

var body: some View {
   
    ZStack{
        Image("background")
            .ignoresSafeArea()

        VStack{
            Spacer()
            Image("logo")
            Spacer()
            HStack{
                Spacer()
                Image(playerCard)
                Spacer()
                Image(cpuCard)
                Spacer()
            }

            Spacer()
            Button(action: {
                
                //Generate random number between 2 and 14
                let playerRand = Int.random(in: 2...14)
                let cpuRand = Int.random(in: 2...14)
                
                //Update the cards
                playerCard = "card" + String(playerRand)
                cpuCard = "card" + String(cpuRand)
                
                //Update the score
                cpuScore += 1
                
                //print("Deal button...")
            }, label: {
                Image("dealbutton")
            })
            Spacer()

            HStack{
                Spacer()
                VStack{
                    Text("Player")
                        .font(.headline)
                        .foregroundColor(Color.white)
                        .padding(.bottom, 10.0)
                    Text(playerScore)
                        .font(.largeTitle)
                        .foregroundColor(Color.white)
                }
                Spacer()
                VStack{
                    Text("CPU")
                        .font(.headline)
                        .foregroundColor(Color.white)
                        .padding(.bottom, 10.0)
                    Text(cpuScore)
                        .font(.largeTitle)
                        .foregroundColor(Color.white)
                }
                Spacer()
            }
            Spacer()
        }
    }
}

}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

…"

Can anyone help me ?
Thx
Arnaud.

Hi @Arnaud

Welcome to the Community.

In your Text() for your playerScore and your cpuScore you need to use String interpolation like this:

Text("\(playerScore)")

or convert the value to a String like this:

Text(String(playerScore))

Also in your Button action code you will need to test for which random number is greater than the other and update the relevant score.

Yes, thanks @Chris_Parker , you’re right… I must be blind :upside_down_face:

1 Like