Failed to produce diagnostic for expression in SwiftUI, XCode 13.2

Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project

This is how my ContentView.swift looks like:

import SwiftUI
struct ContentView: View {
  @State var computersChoice = Int.random(in: 0...2)
  @State var winOrLose = Bool.random()
  @State var score = 0
   
  var options: [Option] = [
    Option(name: "scissors", winMatch: "paper", loseMatch: "rock"),
    Option(name: "rock", winMatch: "scissors", loseMatch: "paper"),
    Option(name: "paper", winMatch: "rock", loseMatch: "scissors")
  ]
   
  var body: some View {
    ZStack {
      LinearGradient(colors: [Color(red: 0.38, green: 0.26, blue: 0.52), Color(red: 0.32, green: 0.39, blue: 0.58)], startPoint: .top, endPoint: .bottom)
        .edgesIgnoringSafeArea(.all)
      VStack(spacing: 30) {
        Spacer(minLength: 45)
        VStack {
           
          Text("Rock Paper Scissors")
            .font(.title)
            .fontWeight(.bold)
            .foregroundColor(.black)
            .padding()
           
           
          VStack(alignment: .center, spacing: 10) {
            Text("Computers Choice: \(options[computersChoice].name)")
              .fontWeight(.bold)
              .padding
            Image("\(options[computersChoice].name)")
              .resizable()
              .frame(width: 100, height: 100)
              .padding()
            Text("You need to \(winOrLose ? "Win" : "Lose")")
              .fontWeight(.bold)
          }
           
        }
        .frame(width: 300, height: 300, alignment: .center)
        .padding()
        .background(.thinMaterial)
        .clipShape(RoundedRectangle(cornerRadius: 6))
        VStack {
          Text("Choose your option:")
            .font(.title3)
            .fontWeight(.bold)
            .foregroundColor(.white)
          Spacer(minLength: 30)
          HStack(spacing: 30) {
            Button {
              didGetCorrect(choice: options[1], winOrNot: winOrLose)
              winOrLose.toggle()
              computersChoice = Int.random(in: 0...2)
            } label: {
              Image("rock")
                .resizable()
                .frame(width: 100, height: 100)
            }
            Button {
              didGetCorrect(choice: options[2], winOrNot: winOrLose)
              winOrLose.toggle()
              computersChoice = Int.random(in: 0...2)
            } label: {
              Image("paper")
                .resizable()
                .frame(width: 100, height: 100)
            }
            Button {
              didGetCorrect(choice: options[0], winOrNot: winOrLose)
              winOrLose.toggle()
              computersChoice = Int.random(in: 0...2)
            } label: {
              Image("scissors")
                .resizable()
                .frame(width: 100, height: 100)
            }
          }
          Spacer(minLength:50)
        }
        VStack {
          Text("Score: \(score)")
            .fontWeight(.heavy)
            .padding()
        }
        .background(
          .thinMaterial,
          in: RoundedRectangle(cornerRadius: 8, style: .continuous)
        )
        Spacer(minLength: 100)
      }
    }
  }
  func didGetCorrect(choice: Option, winOrNot: Bool) {
    if winOrNot {
      score += options[computersChoice].loseMatch == choice.name ? 1 : -1
    } else {
      score += options[computersChoice].winMatch == choice.name ? 1 : -1
    }
  }
}

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

Accepted Reply

You should better include clear sentences explaining what you want to ask here.

Have you come here to just report your bug? Or want to find a solution to fix the bug?

Anyway, your code lacks one pair of parentheses at about 32nd line:

          VStack(alignment: .center, spacing: 10) {
            Text("Computers Choice: \(options[computersChoice].name)")
              .fontWeight(.bold)
              .padding //<-
            Image("\(options[computersChoice].name)")
              .resizable()
              .frame(width: 100, height: 100)
              .padding()
            Text("You need to \(winOrLose ? "Win" : "Lose")")
              .fontWeight(.bold)
          }

So, you need to fill parentheses there:

                    VStack(alignment: .center, spacing: 10) {
                        Text("Computers Choice: \(options[computersChoice].name)")
                            .fontWeight(.bold)
                            .padding() //<-
                        Image("\(options[computersChoice].name)")
                            .resizable()
                            .frame(width: 100, height: 100)
                            .padding()
                        Text("You need to \(winOrLose ? "Win" : "Lose")")
                            .fontWeight(.bold)
                    }

Usual compilers can detect where this sort of simple syntax error is occurring, you can send a bug report to swift.org as suggested.

  • Yes, i actually wanted to fix the issue,Thanks for the help!

Add a Comment

Replies

You should better include clear sentences explaining what you want to ask here.

Have you come here to just report your bug? Or want to find a solution to fix the bug?

Anyway, your code lacks one pair of parentheses at about 32nd line:

          VStack(alignment: .center, spacing: 10) {
            Text("Computers Choice: \(options[computersChoice].name)")
              .fontWeight(.bold)
              .padding //<-
            Image("\(options[computersChoice].name)")
              .resizable()
              .frame(width: 100, height: 100)
              .padding()
            Text("You need to \(winOrLose ? "Win" : "Lose")")
              .fontWeight(.bold)
          }

So, you need to fill parentheses there:

                    VStack(alignment: .center, spacing: 10) {
                        Text("Computers Choice: \(options[computersChoice].name)")
                            .fontWeight(.bold)
                            .padding() //<-
                        Image("\(options[computersChoice].name)")
                            .resizable()
                            .frame(width: 100, height: 100)
                            .padding()
                        Text("You need to \(winOrLose ? "Win" : "Lose")")
                            .fontWeight(.bold)
                    }

Usual compilers can detect where this sort of simple syntax error is occurring, you can send a bug report to swift.org as suggested.

  • Yes, i actually wanted to fix the issue,Thanks for the help!

Add a Comment