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

Trouble with Challenge 3 of Project 2(Guess The Flag)

Forums > SwiftUI

The third challenge was to make so if the user gives an incorrect flag guess, the alert message will display something like "Wrong, that's France's flage" or something like that.

I tried putting the alert modifier inside an if/else statement but I can't get it to work. I'm getting "function declares an opaque return type" and "cannot convert return expression of type 'ZStack' . . . " Any better ways? I'm new to Swift and programming in general so any help would be awesome!

Thanks!

import SwiftUI

struct ContentView: View {
    @State var countries = ["Estonia", "France", "Germany", "Ireland", "Italy", "Poland", "Russia", "Spain", "UK", "US",].shuffled()
    @State var correctAnswer = Int.random(in: 0...2)
    @State private var showingScore = false
    @State private var scoreTitle = ""
    @State private var userScore = 0

    var body: some View {
        ZStack {
            LinearGradient(gradient: Gradient(colors: [.blue, .black]), startPoint: .top, endPoint: .bottom)
                .edgesIgnoringSafeArea(.all)
            VStack(spacing: 30)
            {
                VStack {
                    Text("Tap the flag of ")
                        .foregroundColor(.white)
                    Text(countries[correctAnswer])
                        .font(.largeTitle)
                        .fontWeight(.black)
                        .foregroundColor(.white)
                }
                ForEach(0 ..< 3) { number in
                    Button(action: {
                        self.flagTapped(number)
                    }) {
                        Image(self.countries[number])
                            .renderingMode(.original)
                            .clipShape(Capsule())
                            .overlay(Capsule().stroke(Color.black, lineWidth: 1))
                            .shadow(color: .black, radius: 2)
                    }
                }
                VStack {
                    Text("Current Score: \(String(userScore))")
                        .foregroundColor(.white)
                }
                Spacer()
            }
            if scoreTitle == "Wrong" {
                .alert(isPresented: $showingScore) {
                    Alert(title: Text(scoreTitle), message: Text("Wrong! That's \(countries[number])'s flag. Your score is \(String(userScore))"), dismissButton: .default(Text("Continue")) {
                        self.askQuestion()
                        })
                }
            } else {
                .alert(isPresented: $showingScore) {
                    Alert(title: Text(scoreTitle), message: Text("Your score is \(String(userScore))"), dismissButton: .default(Text("Continue")) {
                        self.askQuestion()
                        })
                }
            }
        }
    }

    func flagTapped(_ number: Int) {
        if number == correctAnswer {
            scoreTitle = "Correct"
            userScore += 1
        }
        else {
            scoreTitle = "Wrong"
        }
        showingScore = true
    }

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

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

3      

Hi after the if you can not you need a View which the .alert is not. You could add

@State private var scoreMessage = ""

then replace the if statement with

.alert(isPresented: $showingScore) {
    Alert(title: Text(scoreTitle), message: Text(scoreMessage), dismissButton: .default(Text("Continue")) {
        self.askQuestion()
    })
 }

and in the

func flagTapped(_ number: Int)

change the scoreMessage to correct or wrong messsage

Hope that helps

3      

Thanks @NigelGee that worked!

3      

I solved it this way..

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

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.