Learn Courses My Dashboard

Lesson 11 (State properties) - error with changing playerScore and cpuScore

Hi all,

Cant seem to emulate the code in Lesson 11 State Properties. many thanks!

Button(action: {
                    //Update the cards
                    playerCard = "card11"
                    cpuCard = "card12"
                    
                    //Update the score
                    playerScore += 1 error message (/Users/jaijordan/Downloads/Image Assets/test/test/ContentView.swift:42:33: '+=' is not a prefix unary operator_
                    cpuScore += 1 error message (/Users/jaijordan/Downloads/Image Assets/test/test/ContentView.swift:42:33: '+=' is not a prefix unary operator)

or entire code is:

//
//  ContentView.swift
//  test
//
//  Created by Jai Jordan on 10/4/21.
//

import SwiftUI

struct ContentView: View {
    
    @State var playerCard = "card5"
    @State var cpuCard = "card9"
    @State var playerScore = "0"
    @State var cpuScore = "0"
    
    var body: some View {
        
        ZStack(content: {
            
            Image("background").ignoresSafeArea()
            
            VStack(content: {
                Spacer()
                Image("logo")
                Spacer()
                HStack(content: {
                    Spacer()
                    Image(playerCard)
                    Spacer()
                    Image(cpuCard)
                    Spacer()
                })
                Spacer()
                
                Button(action: {
                    //Update the cards
                    playerCard = "card11"
                    cpuCard = "card12"
                    
                    //Update the score
                    playerScore += 1
                    cpuScore += 1
                    
                }, label: {Image("dealbutton")
                })
                
                Spacer()
                HStack {
                    Spacer()
                    VStack{
                        Text("Player")
                            .font(.headline)
                            .fontWeight(.medium)
                            .foregroundColor(Color.white)
                            .lineLimit(10)
                            .padding(.bottom)
                        Text(String(playerScore))
                            .font(.subheadline)
                            .fontWeight(.medium)
                            .foregroundColor(Color.white)
                    }
                    Spacer()
                    VStack{
                        Text("CPU")
                            .font(.headline)
                            .fontWeight(.medium)
                            .foregroundColor(Color.white)
                            .lineLimit(10)
                            .padding(.bottom)
                        Text(String(cpuScore))
                            .font(.subheadline)
                            .fontWeight(.medium)
                            .foregroundColor(Color.white)
                    }
                    Spacer()
                }
                Spacer()
            })
        })
    }
}

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

Change your State property for the playerScore and the cpuScore to = 0 in each case rather than = “0”

@State var playerScore = "0"

defines playerScore to be of type String. You can’t perform math operations on a String.

@State var playerScore = 0

defines playerScore to be of type Int which is what you wan so that you can perform math operations on it.

1 Like

Amazing, thank you so much!