UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Question on Day 24 Challenge 2

Forums > 100 Days of SwiftUI

Hi guys,

In the second challenge on day 24 I've written this code:

struct FlagImage: View {
    var image: Image

    var body: some View {

        image
            .renderingMode(.original)
            .clipShape(Capsule())

            .shadow(radius: 5)

    }
}

Probably I didn't understand well what I have to do because it gives me an error when I use it in the Content View.

The error says: "Cannot convert value of type 'String' to expected argument type 'Image'"

struct ContentView: View {

    @State private var showingScore = false
    @State private var showingReset = false
    @State private var scoreTitle = ""
    @State private var userScore = 0

    @State private var countries = ["Estonia", "France", "Germany", "Ireland", "Italy", "Nigeria", "Poland", "Russia", "Spain", "UK", "US"].shuffled()

    @State private var correctAnswer = Int.random(in: 0...2)

    @State private var questionNumber = 1

    var body: some View {
        ZStack {

            RadialGradient(stops: [
                .init(color: Color(red: 0.1, green: 0.2, blue: 0.45), location: 0.3),
                .init(color: Color(red: 0.76, green: 0.15, blue: 0.26), location: 0.3)
            ], center: .top, startRadius: 200, endRadius: 700)
            .ignoresSafeArea()

            VStack {
                Spacer()

                Text("Guess the Flag")
                    .font(.largeTitle.weight(.bold))
                    .foregroundColor(.white)

                VStack(spacing: 15) {
                    VStack {
                        Text("Tap the flag of")
                            .foregroundStyle(.secondary)
                            .font(.subheadline.weight(.heavy))
                        Text(countries[correctAnswer])
                            .font(.largeTitle.weight(.semibold))
                    }

                    ForEach(0..<3) { number in
                        Button {

                            flagTapped(number)
                        } label: {
                            FlagImage(image: countries[number])

                        }
                    }
                }

                .frame(maxWidth: .infinity)
                .padding(.vertical, 20)
                .background(.regularMaterial)
                .clipShape(RoundedRectangle(cornerRadius: 20))

                Spacer()
                Spacer()

                Text("Score: \(userScore)")
                    .foregroundColor(.white)
                    .font(.title.bold())

                Spacer()
            }
            .padding()
        }
        .alert(scoreTitle, isPresented: $showingScore) {
            Button("Continue", action: askQuestion)
        } message: {
            Text("Your score is \(userScore)")
                .foregroundColor(.white)
                .font(.title.bold())
        }
        .alert(scoreTitle, isPresented: $showingReset) {
            Button("Restart", action: reset)
        } message: {
            Text("Your final score is \(userScore)")
                .foregroundColor(.white)
                .font(.title.bold())
        }

    }

    func flagTapped(_ number: Int) {
        if number == correctAnswer {
            scoreTitle = "Correct"
            userScore += 1
        } else {
            scoreTitle = "Wrong That's the flag of \(countries[number])"
        }

        if questionNumber != 8 {
            showingScore = true
        } else {
            showingScore = false
            showingReset = true
            scoreTitle = "Endgame"
        }
    }

    func askQuestion() {
        countries.shuffle()
        correctAnswer = Int.random(in: 0...2)
        questionNumber += 1
    }

    func reset() {
        countries.shuffle()
        correctAnswer = Int.random(in: 0...2)
        userScore = 0
    }

}

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

I'll wait for your responses. Thanks.

2      

In FlagImage you're asking for an image but you're giving it a string.

struct FlagImage: View {
    var image: Image // You're asking for an image

    var body: some View {

        image
            .renderingMode(.original)
            .clipShape(Capsule())

            .shadow(radius: 5)

    }
}
 ForEach(0..<3) { number in
                        Button {

                            flagTapped(number)
                        } label: {
                            FlagImage(image: countries[number]) // You're passing a string, not an image
                        }
                    }

To solve the problem, change FlagImage:

struct FlagImage: View {
    var imageName: String // Ask for a string

    var body: some View {

        Image(imageName) // Use the imageName to lookup the flag and create an image
            .renderingMode(.original)
            .clipShape(Capsule())

            .shadow(radius: 5)

    }
}
ForEach(0..<3) { number in
                        Button {

                            flagTapped(number)
                        } label: {
                            FlagImage(imageName: countries[number])
                        }
                    }

2      

Thank you so much! Now I can go on with the course.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.