War Card Game - Issue with 'playerCard' in scope

I am at Module 1 Lesson 8 and using Xcode version 15.4.

Somehow even after using @State, I still get "Cannot find ‘playerCard’ in scope. Can you please tell me how I can fix it?

import SwiftUI

struct ContentView: View {
    var body: some View {

        @State var playerCard = "card7"
        @State var cpuCard = "card12"

        @State var playerScore: Int = 0
        @State var cpuScore: Int = 0

        ZStack{
            Image("background-plain")
                .resizable()
                .ignoresSafeArea()
            VStack {
                Image("logo")
                Spacer()
                    HStack{
                        Spacer()
                        Image(playerCard)
                        Spacer()
                        Image(cpuCard)
                        Spacer()
                    }
                Spacer()

                Button{
                    deal()
                } label: {
                    Image("button")
                }

                Spacer()
                    HStack{
                        Spacer()
                        VStack{
                        
                            Text("Player")

                            Text(String(playerScore))

                        }
                        .foregroundColor(Color.orange)
                        .font(.title3)
                        Spacer()
                        VStack{

                            Text("CPU")

                            Text(String(cpuScore))

                        }
                        .foregroundColor(Color.orange)
                        .font(.title3)
                        Spacer()

                    }
                Spacer()
            }
        }
    }
    func deal(){
        // Randomize a player card
        playerCard = "card" + String(Int.random(in: 2...14))

        // Randomize a CPU card
        cpuCard = "card" + String(Int.random(in: 2...14))

        // Update the scores
    }
}

#Preview {
    ContentView()
}

Hi @pohnson,

Welcome to the community.

Move these 4 declarations to before the var body: some View {

        @State var playerCard = "card7"
        @State var cpuCard = "card12"

        @State var playerScore: Int = 0
        @State var cpuScore: Int = 0

ie, it should be like this:

struct ContentView: View {
    @State var playerCard = "card7"
    @State var cpuCard = "card12"

    @State var playerScore: Int = 0
    @State var cpuScore: Int = 0
    
    var body: some View {


            ZStack{
                Image("background-plain")
                    .resizable()
                    .ignoresSafeArea()
                VStack {
                    Image("logo")
                    Spacer()
                        HStack{
                            Spacer()
                            Image(playerCard)
                            Spacer()
                            Image(cpuCard)
                            Spacer()
                        }
                    Spacer()

                    Button{
                        deal()
                    } label: {
                        Image("button")
                    }

                    Spacer()
                        HStack{
                            Spacer()
                            VStack{

                                Text("Player")

                                Text(String(playerScore))

                            }
                            .foregroundColor(Color.orange)
                            .font(.title3)
                            Spacer()
                            VStack{

                                Text("CPU")

                                Text(String(cpuScore))

                            }
                            .foregroundColor(Color.orange)
                            .font(.title3)
                            Spacer()

                        }
                    Spacer()
                }
            }
        }
        func deal(){
            // Randomize a player card
            playerCard = "card" + String(Int.random(in: 2...14))

            // Randomize a CPU card
            cpuCard = "card" + String(Int.random(in: 2...14))

            // Update the scores
        }
}

it works. Thank you Chris.

1 Like