Learn Courses My Dashboard

Buttons not printing to console or updating UI

I had a question about lesson 10 (buttons). I’ve looked at a lot on the internet trying to get my console to display the action: { print (“hello world”), yet nothing is working (the surrounding code is correct since I copied it from Chris’ video). Here some of thing things I’ve tried:

1 restarting Xcode and my computer
2 opening and closing the debugging console in different ways
3 making sure the correct Xcode behaviors boxes are checked

Here is my code:

import SwiftUI

struct ContentView: View {

//can reference in any part of this structure
@State var playerCard = "card5"
@State var cpuCard = "card9"
@State var playerScore = 0
@State var cpuScore = 0
//get notification of changes and update automatically

var body: some View {
    ZStack {
        
        Image("background")            .ignoresSafeArea()
        //start of actual stuff
        VStack{
        
        Spacer()
        Image("logo")
            
        //cards
        Spacer()
            HStack{
        Spacer()
        Image(playerCard)
        Spacer()
        Image(cpuCard)
        Spacer()
            }
            
        Spacer()
            
        //make button
            Button(action: {
                //update the cards
                print("hello world")
                playerCard = "card11"
                cpuCard = "card12"
                //need a property wrapper
                //state property wrapper
                
                //update the score
                playerScore+=1
                cpuScore+=1
                
                
                
            }
                   , label: {
                    Image("dealbutton")
            })
            
            
            
        Spacer()
            VStack{
        HStack{
        Spacer()
            Text("Player").font(.headline).fontWeight(.bold).foregroundColor(.white).padding(.bottom, 10.0)
        Spacer()
        
            Text("CPU").font(.headline).fontWeight(.bold).foregroundColor(.white).padding(.bottom, 10.0)

        Spacer()
            
        }
                HStack{
                    Spacer()
                    Text(String(playerScore)).font(.largeTitle).foregroundColor(.white)

                    Spacer()
                    //pass in int into string
                    Text(String(cpuScore)).font(.largeTitle).foregroundColor(.white)

                    Spacer()
                }
        
    }
        Spacer()
}

}
}
}

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

Ive just tested out your code in the simulator and it all works fine. Take a look at the very bottom right of Xcode and make sure the ‘Hide console’ button is selected to bring up the right hand console where the info is printed.

The “hide console” button is selected and it still doesn’t print out anything or update the playing cards in the war game UI.

Possibly create a new project and copy your code into it and see if that helps. Sometimes Xcode is buggy like that.

hi, everyone,

has somebody figured this out? I’m on module 2 lesson 2 challenge and whenever I click my button, nothing comes out on the console. I would really appreciate the help and the enlightenment on this.

Much love and thanks,
Mage

PS: I’ve tried Moonz suggestion to copy and paste my code on a new project. It still didn’t work.

@marygraceona
Hi Mage, hope you are having a nice day. I’m just wondering if you are pressing the run button in the Xcode menu bar or pressing the play for the previews…

The Preview does not print to the Console. You need to “Run” the project on a simulator.

I’m doing it via the preview. Will check it using the simulator. Thanks, guys! :slightly_smiling_face:

Got it! Will check this later. Thanks much! :slightly_smiling_face: