hey
every time I write button then it comes with other things I can write but not the right command
you can see what I mean with the screenshot I have Attached
hope you can help me
thanks for the good course
-Gustav
hey
every time I write button then it comes with other things I can write but not the right command
you can see what I mean with the screenshot I have Attached
hope you can help me
thanks for the good course
-Gustav
Hi Gustav,
Welcome to the code crew community. I assume that you are following the “14 Day Beginner Challenge (SwiftUI)”. Is that correct?
Can you show a screenshot of all the code in the View you are working on. Better still, can you copy the code from the Xcode window by selecting all of it and press Command + C to copy and then paste the code in a reply.
To format the code you should select that code block in the reply (just the code you pasted in) and then tap on the </> icon in the toolbar of the message composer window.
yes I follow the 14 Day Beginner Challenge but on skillshare
type or import SwiftUI
struct ContentView: View {
var body: some View {
ZStack {
Image("bag") .edgesIgnoringSafeArea(.all)
VStack {
Spacer()
Image("logo")
Spacer()
HStack {
Spacer()
Image("card2")
Spacer()
Image("card3")
Spacer()
}
Spacer()
Image ("Deal")
Spacer()
HStack {
Spacer()
VStack {
Text("player")
.font(.headline)
.foregroundColor(Color.white)
.padding(.bottom, 10.0)
Text("0")
.font(.largeTitle)
.foregroundColor(Color.white)
}
Spacer()
VStack {
Text("cpu")
.font(.headline)
.foregroundColor(Color.white)
.padding(.bottom, 10.0)
Text("0")
.font(.largeTitle)
.foregroundColor(Color.white)
}
Spacer()
}
Spacer()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
}
paste code here
here is the full code
but I can't get the settings up after I type "button(" so when I do it its just looks like a new line
One thing you must always do is to reformat your code in the Xcode editing window so that the code indenting is renewed. That helps you identify if there are missing opening { and closing } braces or incorrectly positioned opening and closing braces.
To reformat your code, select it all Command + A
and then press Shift + I
.
When you specify a button, type the first letter as a Capital. So you would type:
Button(
and the autocomplete will give you some options like this screenshot:
In this case I chose the first one (it may be different for you) and this is how it looks when you choose that code:
Here is your code with a Button added and uses the “dealbutton” image from the assets.
struct ContentView: View {
var body: some View {
ZStack {
Image("background")
.resizable()
.edgesIgnoringSafeArea(.all)
VStack {
Spacer()
Image("logo")
Spacer()
HStack {
Spacer()
Image("card2")
Spacer()
Image("card3")
Spacer()
}
Button {
// action code goes here.
} label: {
Image ("dealbutton")
}
Spacer()
HStack {
Spacer()
VStack {
Text("player")
.font(.headline)
.foregroundColor(Color.white)
.padding(.bottom, 10.0)
Text("0")
.font(.largeTitle)
.foregroundColor(Color.white)
}
Spacer()
VStack {
Text("cpu")
.font(.headline)
.foregroundColor(Color.white)
.padding(.bottom, 10.0)
Text("0")
.font(.largeTitle)
.foregroundColor(Color.white)
}
Spacer()
}
.padding(.bottom)
}
}
}
}
thanks for your help Chris
it works
sorry I was so long to reply to you
i have a new problem I can’t get the cpu and player score to stand the right way
i have put a screenshot about what I mean
hope you can help me again😀
Both the VStacks containing each score should be inside a HStack. Like this with a Spacer() between each VStack so each of the score VStacks are pushed out to the edge.:
HStack {
VStack {
Text("player")
.font(.headline)
.foregroundColor(Color.white)
.padding(.bottom, 10.0)
Text("0")
.font(.largeTitle)
.foregroundColor(Color.white)
}
Spacer()
VStack {
Text("cpu")
.font(.headline)
.foregroundColor(Color.white)
.padding(.bottom, 10.0)
Text("0")
.font(.largeTitle)
.foregroundColor(Color.white)
}
}
but if I use the code then the scores won’t go up I don’t know why
import SwiftUI
struct ContentView: View {
@State private var PlayerCard = ("card11")
@State private var cpuCard = ("card13")
@State private var playerScore = 0
@State private var cpuScore = 0
var body: some View {
ZStack {
Image("bag")
.resizable()
.edgesIgnoringSafeArea(.all)
VStack {
Spacer()
Image("logo")
Spacer()
HStack {
Spacer()
Image(PlayerCard)
Spacer()
Image(cpuCard)
Spacer()
}
Button {
// random number
let playerRand = Int.random(in:2...14)
let cpuRand = Int.random(in: 2...14)
// action code goes here.
PlayerCard = "card" + String (playerRand)
cpuCard = "card" + String (cpuRand)
// next action code here
if
playerRand > cpuRand {
playerScore += 1
}
else if cpuRand > playerRand {
cpuScore += 1
}
} label: {
Image ("Deal")
}
Spacer()
HStack {
Spacer()
VStack {
HStack {
VStack {
Text("player")
.font(.headline)
.foregroundColor(Color.white)
.padding(.bottom, 10.0)
Text("0")
.font(.largeTitle)
.foregroundColor(Color.white)
Spacer()
VStack {
Spacer()
}
}
Spacer()
VStack {
Text("cpu")
.font(.headline)
.foregroundColor(Color.white)
.padding(.bottom, 10.0)
Text("0")
.font(.largeTitle)
.foregroundColor(Color.white)
}
}
}
.padding(.bottom)
}
}
}
}
}
this is with the code you sent me and it doesn’t work
Substitute the Text(“0”) in each VStack with the playerScore and cpuScore in their respective positions. Use string interpolation or wrap each value is a String() to convert the integer value to a String.
i am so sorry but I cant get it to work
can you show me how with a example of it
sorry for the inconvenience
In the case of the Player, instead of Text(“0”) you would have Text("(playerScore)"). This is called string interpolation where you have a string which is anything between to double quotes ""
where in this case you want the playerScore to appear so you wrap that playerScore inside \()
. ie:
Text("\(playerScore)")
Do the same for cpuScore.
In your button code you already have the logic that creates the random numbers for playerRand and cpuRand and you are incrementing the playerScore where the playerRand is greater than the cpuRand and if the cpuRand is greater than the playerRand you are incrementing the cpuScore.