How to fix these errors?

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 {
    let viewModel: EmojiMemoryGame

    var body: some View {

        VStack {
                LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {
                    ForEach(viewModel.cards) { card in
                        CardView(card: card)
                            .aspectRatio(aspectRatio, contentMode: .fit) // here's the first error which says "Cannot convert value of type '(CGFloat?, ContentMode) -> some View' to expected argument type 'CGSize'"
                    }
                }
                .padding()
                .foregroundColor(Color(.systemTeal))
                
                Spacer()

                Button {
                    emojiCount += 1
                    if emojiCount == 10 {
                        aspectRatio = 9/3
                    }
ì
                    if emojiCount > 17 {
                        print("Card finished")

                    }

                } label: {

                    Text("Add Card")

                }
            }
        }
    }


struct CardView: View {

    let card: MemoryGame<String>.Card

    

    var body: some View {

        ZStack {

            let shape = RoundedRectangle(cornerRadius: 20)

            if card.isFaceUp {

                shape.fill().foregroundColor(.white)

                shape.stroke(lineWidth: 3)

                Text(card.content).font(.system(size: 50))

            } else {

                shape.fill(.orange)

            }

        }
    }

}

struct ContentView_Previews:
    PreviewProvider {
        static var previews: some View {
            ContentView(viewModel: game) //here's the second error which says "Cannot find 'game' in scope"
    }
}
Answered by Claude31 in 702300022

You can get the code here: https://cs193p.sites.stanford.edu

by downloading L7+L8 Demo Code(at the bottom of page)

I noted for instance that in Preview, they define:

        let game = EmojiMemoryGame()

Could you tell what EmojiMemoryGame is ?

For first error, did you try replacing by

                    ForEach(viewModel.cards) { card in
                        CardView22(card: card)
                            .aspectRatio(aspectRatio, contentMode: ContentMode.fit) 

For the second, where is game defined?

It says me "Cannot find 'CardView22' in scope", maybe because we haven't defined the CardView22. This is EmojiMemoryGame:

import SwiftUI

class EmojiMemoryGame {

    static let emojis = ["😀", "😍", "🤪", "😱", "🥶", "🤑", "😡", "🤠", "🥳", "🤩", "🥴", "😴", "😪", "😶‍🌫️", "😈", "🤯", "🤐", "🤗"]

    static func createMemoryGame() -> MemoryGame<String> {
        MemoryGame<String>(numberOfPairsOfCards: 4) { pairIndex in
            emojis[pairIndex]
        }
    }
    private var model: MemoryGame<String> = createMemoryGame()

    var cards: Array<MemoryGame<String>.Card> {

        model.cards
    }
}

I have defined "game" here:

import SwiftUI



@main 

struct MemorizeApp: App {

    let game = EmojiMemoryGame()

    

    var body: some Scene {

        WindowGroup {

            ContentView(viewModel: game)

        }
    }
}

You have not defined "aspectRatio"

Now I have defined it but despite it gives me the same errors Code:

import SwiftUI


struct ContentView: View {

    let viewModel: EmojiMemoryGame

    var body: some View {

        VStack {

                LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {

                    ForEach(viewModel.cards) { card in

                        CardView(card: card).aspectRatio(aspectRatio, contentMode: .fit)  //Cannot convert value of type '(CGFloat?, ContentMode) -> some View' to expected argument type 'CGSize'

                    }

                }

                .padding()

                .foregroundColor(Color(.systemTeal))

                

                Spacer()

                

                Button {

                    emojiCount += 1

                    if emojiCount == 10 {

                        aspectRatio = 9/3

                    }

                    if emojiCount > 17 {

                        print("Card finished")

                    }

                } label: {

                    Text("Add Card")

                }

            }

        }

    

    }





struct CardView: View {

    let card: MemoryGame<String>.Card

    

    var body: some View {

        ZStack {

            let shape = RoundedRectangle(cornerRadius: 20)

            if card.isFaceUp {

                shape.fill().foregroundColor(.white)

                shape.stroke(lineWidth: 3)

                Text(card.content).font(.system(size: 50))

            } else {

                shape.fill(.orange)

            }

        }

        

    }

}





struct ContentView_Previews:

    PreviewProvider {

        static var previews: some View {

            ContentView(viewModel: game) //Cannot find 'game' in scope

    }

}

When you use aspectRatio, it is not defined. You only define for == 10.

                if emojiCount == 10 {
                    aspectRatio = 9/3
                }

So add an initial value to it, for instance = 1.

However, I do not see where aspect ratio is declared.

PS: when you paste code use Paste And Match Style to avoid all the extra lines that make code hard to read.

import SwiftUI

struct ContentView: View {
    
    let viewModel: EmojiMemoryGame
    
    var body: some View {
        VStack {
            LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {
                ForEach(viewModel.cards) { card in
                    CardView(card: card).aspectRatio(aspectRatio, contentMode: .fit)  //Cannot convert value of type '(CGFloat?, ContentMode) -> some View' to expected argument type 'CGSize'
                }
            }
            .padding()
            .foregroundColor(Color(.systemTeal))
            
            Spacer()
            
            Button {
                emojiCount += 1
                if emojiCount == 10 {
                    aspectRatio = 9/3
                }
                
                if emojiCount > 17 {
                    print("Card finished")
                }
                
            } label: {
                Text("Add Card")
            }
            
        }
        
    }
    
}


struct CardView: View {

    let card: MemoryGame<String>.Card

    var body: some View {

        ZStack {
            let shape = RoundedRectangle(cornerRadius: 20)
            if card.isFaceUp {
                shape.fill().foregroundColor(.white)
                shape.stroke(lineWidth: 3)
                Text(card.content).font(.system(size: 50))
            } else {
                shape.fill(.orange)
            }

        }

    }

}


struct ContentView_Previews: PreviewProvider {

        static var previews: some View {
            ContentView(viewModel: game) //Cannot find 'game' in scope
    }

}

I tried to copy and paste your code, but it's still not working. I really don't know how to figure out this

For testing purpose, try:

                    CardView(card: card).aspectRatio(2.0, contentMode: ContentMode.fit) 

Yes I was responding to you. It seams that you changed something, I'm sorry. Now I have tried to put CardView(card: card).aspectRatio(2.0, contentMode: ContentMode.fit) into my code and then put @State before aspectRatio. So this the updated code:

import SwiftUI



struct ContentView: View {

    let viewModel: EmojiMemoryGame
    @State var aspectRatio = 1

    var body: some View {

        let emojis = ["😀", "😍", "🤪", "😱", "🥶", "🤑", "😡", "🤠", "🥳", "🤩", "🥴", "😴", "😪", "😶‍🌫️", "😈", "🤯", "🤐", "🤗"]     //WARNING: Initialization of immutable value 'emojis' was never used; consider replacing with assignment to '_' or removing it. Replace 'let emojis' with '_'

        var emojiCount = 4

        VStack {
            LazyVGrid(columns: [GridItem(), GridItem(), GridItem()]) {
                ForEach(viewModel.cards) { card in
                    CardView(card: card).aspectRatio(2.0, contentMode: ContentMode.fit)
                }
            }
            .padding()
            .foregroundColor(Color(.systemTeal))

            Spacer()

            Button {
                emojiCount += 1
                if emojiCount == 10 {
                    aspectRatio = 9/3 
                }
                if emojiCount > 17 {
                    print("Card finished")
                }
            } label: {
                Text("Add Card")
            }
        }
    }
}

struct CardView: View {
    let card: MemoryGame<String>.Card

    var body: some View {

        ZStack {

            let shape = RoundedRectangle(cornerRadius: 20)
            if card.isFaceUp {
                shape.fill().foregroundColor(.white)
                shape.stroke(lineWidth: 3)
                Text(card.content).font(.system(size: 50))
            } else {
                shape.fill(.orange)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView(viewModel: game) //Cannot find 'game' in scope
    }
}

Now it gives me one Warning and an Error :(

But could you show where aspectRatio is defined ?

It is very hard to tell anything, as parts of change change at each post ; emojis were initially here

class EmojiMemoryGame {
    static let emojis = ["😀", "😍", "🤪", "😱", "🥶", "🤑", "😡", "🤠", "🥳", "🤩", "🥴", "😴", "😪", "😶‍🌫️", "😈", "🤯", "🤐", "🤗"]

Now they are in ContentView…

As for game, that's an error you had for long. Effectively, it is defined inside a class, and not visible outside

struct MemorizeApp: App {

    let game = EmojiMemoryGame()

    var body: some Scene {

        WindowGroup {
            ContentView(viewModel: game)
        }
    }
}

It works here, but is not in scope of ContentView_Previews

I have defined aspectRatio here:

struct ContentView: View {

    let viewModel: EmojiMemoryGame
    @State var aspectRatio = 1

Ps: I understand that is really hard to keep track of the code in this way but I do the possible to make it easy.

Accepted Answer

You can get the code here: https://cs193p.sites.stanford.edu

by downloading L7+L8 Demo Code(at the bottom of page)

I noted for instance that in Preview, they define:

        let game = EmojiMemoryGame()

Oh thank you very very much! I actually can’t find the code I have searched a lot in the website, I really thank you for the help!

I am on class 2 of memorize game and got "scope" error too.

//start code

import SwiftUI

struct ContentView: View {

    var emoji = ["👩‍🚀","🕵️‍♂️","👩‍🔬","🦾","💃" ,"🦸‍♀️" ,"👨‍🍳" ,"🧑‍🎤" ,"👨‍⚖️" ,"👩‍🌾" ,"👷‍♀️" ,"💂‍♀️" ,"👲" ,"🕴" ,"👩‍🚒" ,"🦹‍♂️" ,"👰‍♀️" ,"👨‍💻" ,"🥷" ,"🙅" ,"🧟‍♂️" ,"🤶" , "🧑‍🎨","🧛‍♂️", "🧞‍♂️","🧌 "]

    

@State var EmojiCounting = 3

    var body: some View {

        VStack{

        HStack{

            ForEach(emoji[0..<EmojiCounting], id: .self) { emoji in CardView(content: emoji)

            }}

            HStack {

                remove

                Spacer()

                add}

            

        .padding(.horizontal)

        .foregroundColor(.red)

        }}}

var remove: some View {

    return Button (action: {EmojiCounting -= 1}, label: {

    VStack {

        Text("Remove")

        Text("Card")

    }})}

var add: some View {

    Button(action: {

    EmojiCounting += 1 }, label: {

    VStack {

        Text("Add")

        Text("Card")

    }})}

struct CardView: View {

    var content: String

  @State var isFaceUp: Bool = true

    

    var body: some View {

        ZStack {

            let shape = RoundedRectangle(cornerRadius: 20)

            if isFaceUp {

            shape.fill().foregroundColor(.white)

            shape.stroke(lineWidth: 3)

            Text(content).font(.largeTitle)

            } else {

                shape.fill()

            }

        }

        

        .onTapGesture {

            isFaceUp = !isFaceUp

        }

        

    }

    

}

        

struct ContentView_Previews: PreviewProvider {

    static var previews: some View {

        ContentView()

            

        }}

        

                

How to fix these errors?
 
 
Q