WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Guess the Flag, only two flags show up

Forums > 100 Days of SwiftUI

Hi there -

Sometimes I only get two flags. Here is my code.

struct ContentView: View {
    @State private var countries = ["Estonia", "France", "Germany", "Ireland", "Italy", "Nigeria", "Poland", "Spain", "Great Britian", "Ukraine", "US"].shuffled()
    @State private var correctAnswer = Int.random(in: 0...2)
    @State private var showingScore = false
    @State private var scoreTitle = ""
    @State private var playerScore = 0
    @State private var selectedFlag = 0
    @State private var questionCount = 1

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

    func askQuestion(){
        correctAnswer = Int.random(in: 0...2)
        questionCount += 1

    var body: some View {
            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)
                Text("Guess the Flag")
                VStack(spacing: 15){
                        Text("Tap the flag of:")

                    ForEach(0..<3){ number in
                        } label: {
                                .shadow(radius: 15)
                .frame(maxWidth: .infinity)
                .padding(.vertical, 20)
                .clipShape(.rect(cornerRadius: 20))
                Text("Score: \(playerScore)")
                Text("Question Count: \(questionCount)")

        .alert(scoreTitle, isPresented: $showingScore){
            Button("Continue", action: askQuestion)
        } message: {
            if scoreTitle == "Correct" {
                Text("Your Score is \(playerScore)")
            } else {
                Text("You chose the flag of \(countries[selectedFlag])")
                Text("Your Score is still \(playerScore)")

I don't know how to post a picture of it, I'll work on that.


HINT: I would bet one whole British farthing that you never see the flag of the British empire.

Keep Coding

Investigate! Then return with your findings.


I don't know how to post a picture of it, I'll work on that.

A forum regular posted these great hints

See-> How to include images in forum posts
See-> Tutorial: Inserting Images into Forum Posts

Note for @twoStraws

If @twoStraws is reading this:

This would be a great project for PinkerStraws!

Hello Paul! Have you considered adding a video in the first 5 days of your 100 days course informing students how to post a question to the forums, including some basic rules and hints. A bonus video might explain how to add a screen shot to the forums! Might even be useful to include how to post code snips. Also students might be interested to view your thoughts on how to post their progress by using ONE message and updating it with daily progress and questions.

With fondness, gratitude, and admiration,

Your HackingWithSwift fans!



Thanks for pointing me in the right direction.

I was not thinking through how everything connected. I did rename UK to Great Britian, forgetting that I did not rename the flag file names as well.

Thanks for the tips about adding pictures. I'll have to figure something out.


Also, if you're going to spell it out, try remembering this school house rhyme:


It's A before I,
else in the dungeons you'll die. 🪦💀

Aaaaah. Grade school. Good memories. 🏰 👻


Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

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.