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

Day 21 guessTheFlag error with default codes

Forums > 100 Days of SwiftUI

hi everyone,

I am doing day 21 guessTheFlag game app challenge. I kept bumping into this error and I am not sure how to resolve it.

here is the code and the bug I kept getting is Result of call to 'alert(isPresented:content:)' is unused.

the first time I coded, I tried doing the code my own way, couldn't resolve the bug. So I thought I'd copy and paste the tutorial codes, the same bug still exists. can someone please see what happened for me? Thank you!

https://ibb.co/LzqHGXS is the image. I highlighted where the bug is and where the var for the bug are present in the whole code.

3      

You've got a number of issues here.

  1. Clean up your indents to make the code easier to follow.
  2. You need to move askQuestion() and flagTapped() out of the body
  3. The .alert is not attached to the correct element.

Also, for the future, you can (and it's better to) post your code directly on the forum. Put backticks ``` on the line before and the line after your code block and you will get nicely formatted Swift code. That makes it not only easier to read than an image, but it's also easier for people to help you if they can simply copy/paste your code to work on in Xcode rather than having to retype it all from an image.

3      

thanks man. I will tidy my codes better and post it on the post. cheers

3      

RoosterBoy is correct

i assume you just started the project. So move the func out of body but still in struct

struct ContentView: View {
    @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 showingScore = false
    @State private var scoreTitle = ""

    var body: some View {
        ZStack {
            LinearGradient(gradient: Gradient(colors: [.blue, .black]), startPoint: .top, endPoint: .bottom)

            VStack(spacing: 30) {
                VStack {
                    Text("Tap the flag of")
                        .foregroundColor(.white)

                    Text(countries[correctAnswer])
                        .foregroundColor(.white)
                        .font(.largeTitle)
                        .fontWeight(.black)
                }
            }

            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)
                }

                Spacer()
            }
        }
        .alert(isPresented: $showingScore) {
            Alert(title: Text(scoreTitle), message: Text("Your score is ???"), dismissButton: .default(Text("Continue")) {
                self.askQuestion()
                })
        }
    }

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

    func flagTapped(_ number: Int) {

        if number == correctAnswer {
            scoreTitle = "Correct"
        } else {
            scoreTitle = "Wrong!"
            showingScore = true   // <-move this
        }
        // to here
    }
}

one other thing you may want to take showingScore = true from the else to after the statement so it will change it even if correct. good luck with rest of it.

PS keeping indents correct helps you adding code in wrong place

4      

I solved it before I saw your reply but thanks for the detail!

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.