Hello there! I want to create a cards game, and I'm creating it following the Stanford University's tutorial. I followed the video step by step but there is a warning that In the video doesn't appear. Can someone help me to fix it? Here's the link of the video: [https://www.youtube.com/watch?v=oWZOFSYS5GE&t=671s) This is my code:
import SwiftUI
struct ContentView: View {
var emojis =["🚲","🚂","🚁","🚜","🚕","🏎","🚑","🚓","🚒","🚁","🚀","⛵️","🛸","🛶","🚌","🛺","🚇","🚠","🦽","🚗","🚚","🛴","🛵","🚅"]
@State var emojiCount: Int = 4
var body: some View {
ScrollView {
LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {
HStack {
ForEach(emojis[0..<emojiCount], id: \.self, content: { emoji in
CardView(content: emoji).aspectRatio(2/3, contentMode:.fit)
})
}
}
.foregroundColor(/*@START_MENU_TOKEN@*/.red/*@END_MENU_TOKEN@*/)
}
Spacer()
HStack {
remove
Spacer()
add
}
.font(.largeTitle)
.padding(.horizontal)
}
}
var remove: some View {
Button {
if emojiCount > 1 {
// here's the first error which says "Cannot find 'emojiCount' in scope'"
emojiCount -= 1
// here's the same error which says "Cannot find 'emojiCount' in scope'"
}
} label: {
Image(systemName: "minus.circle")
}
}
var add: some View {
Button {
if emojiCount < emojis.count {
// here's the same error which says "Cannot find 'emojiCount' in scope'"
emojiCount += 1
// here's the same error which says "Cannot find 'emojiCount' in scope'"
}
} label: {
Image(systemName: "plus.circle")
}
}
struct CardView: View {
var content: String
@State var isFaceUp: Bool = true
var body: some View {
ZStack {
let shape = RoundedRectangle(cornerRadius: 8)
if isFaceUp {
shape.fill(.white)
shape.strokeBorder(lineWidth:3).foregroundColor(.blue)
Text(content).font(.largeTitle)
} else {
shape.fill(.blue)
}
}
.onTapGesture {
isFaceUp = !isFaceUp
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.preferredColorScheme(.dark)
}
}
Do you mean those warnings ?
if emojiCount > 1 {
// here's the first error which says "Cannot find 'emojiCount' in scope'"
emojiCount -= 1
// here's the same error which says "Cannot find 'emojiCount' in scope'"
}
You're new to the forum, so welcome. And get a few advices:
- You'd better expose error messages clearly and not let others search for them in code…
- when you post code to avoid leaving 20 blank lines… Use Paste and Match Style and then code formatter tool. That will make your code easier to read.
I noted several errors:
var emojis = ["🚲","🚂","🚁","🚜","🚕","🏎","🚑","🚓","🚒","🚁","🚀","⛵️","🛸","🛶","🚌","🛺","🚇","🚠","🦽","🚗","🚚","🛴","🛵","🚅"]
There need a space after =
before [
var remove: some View {
is defined out of
struct ContentView: View {
Hence,
@State var emojiCount
is not visible out of its scope (the struct).
Probably, you have a misplaced closing parenthesis.
This should be correct:
import SwiftUI
struct ContentView: View {
var emojis = ["🚲","🚂","🚁","🚜","🚕","🏎","🚑","🚓","🚒","🚁","🚀","⛵️","🛸","🛶","🚌","🛺","🚇","🚠","🦽","🚗","🚚","🛴","🛵","🚅"]
@State var emojiCount: Int = 4
var body: some View {
ScrollView {
LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {
HStack {
ForEach(emojis[0..<emojiCount], id: \.self, content: { emoji in
CardView(content: emoji).aspectRatio(2/3, contentMode:.fit)
})
}
}
.foregroundColor(/*@START_MENU_TOKEN@*/.red/*@END_MENU_TOKEN@*/)
}
Spacer()
HStack {
remove
Spacer()
add
}
.font(.largeTitle)
.padding(.horizontal)
}
// REMOVE HERE ----> }
var remove: some View { // Now, this is in ContentView scope, and emojiCount is in scope
Button {
if emojiCount > 1 {
// here's the first error which says "Cannot find 'emojiCount' in scope'"
emojiCount -= 1
// here's the same error which says "Cannot find 'emojiCount' in scope'"
}
} label: {
Image(systemName: "minus.circle")
}
}
var add: some View {
Button {
if emojiCount < emojis.count {
// here's the same error which says "Cannot find 'emojiCount' in scope'"
emojiCount += 1
// here's the same error which says "Cannot find 'emojiCount' in scope'"
}
} label: {
Image(systemName: "plus.circle")
}
}
} // <------- INSERT HERE
struct CardView: View {
var content: String
@State var isFaceUp: Bool = true
var body: some View {
ZStack {
let shape = RoundedRectangle(cornerRadius: 8)
if isFaceUp {
shape.fill(.white)
shape.strokeBorder(lineWidth:3).foregroundColor(.blue)
Text(content).font(.largeTitle)
} else {
shape.fill(.blue)
}
}
.onTapGesture {
isFaceUp = !isFaceUp
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.preferredColorScheme(.dark)
}
}