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

SOLVED: Project 2, challenge 2 and 3. Help me please. I'm stuck

Forums > 100 Days of Swift

Hi guys,

So I'm new to swift and I'm having some issues with the second challenge... I'm not sure how to fix it. I have written a if statement underneath the func buttontapped and used UIAlertController and UIAlertAction. But when I reach 10, it just continues to count from 10 to 11 and so on. And both the title and the message is not displayed. Also I am having issues with the second challenge as well. It's sort of works, but it doesn't display the correct country for the flag the sender has tapped.

This is my code:

var wrongAnswer = 0 var topScore = 0

@IBAction func buttonTapped(_ sender: UIButton) { var title: String

if sender.tag == correctAnswer {
    title = "Correct"
    score += 1
    let ac = UIAlertController(title: title, message: "Your score is \(score)", preferredStyle: .alert)
    ac.addAction(UIAlertAction(title: "Continue", style: .default, handler: askQuestion))
    present(ac, animated: true)
} else {
    title = "Wrong"
    score -= 1
    let aw = UIAlertController(title: title, message: "Wrong! That's the flag of \(countries[wrongAnswer])", preferredStyle: .alert)
    aw.addAction(UIAlertAction(title: "Continue", style: .default, handler: askQuestion))
    present(aw, animated: false)
}
    if topScore == 10 {
        let at = UIAlertController(title: "10 guesses done", message: "You have reached the end of this game, your final Score is \(score)", preferredStyle: .alert)
       at.addAction(UIAlertAction(title: "Restart", style: .default, handler: askQuestion))
        present(at, animated: true)
    }

    I would be very happy if someone could help me. :)

3      

But when I reach 10, it just continues to count from 10 to 11 and so on.

Because there's no code there to reset the score to zero or stop the game or whatever you want to do when the user score hits 10. You display an alert but then what?

3      

@roosterboy

Thanks for the response, that takes care of the last challenge. But what is wrong with my code on the 2 challenge? Well I know what is wrong, but how do I fix it? I want the wrongAnswer variable to be the button/flag the user touch when selecting the flag... I feel so stupid not be able to solve this... :( I tried to think the same way as with correctAnswer but have not yet cracked that code.

3      

It looks like wrongAnswer is 0. If you want to capture the button touch, you'll want to grab the Int from sender.tag, which is passed when a button is pressed. Then you can use the countries array to see what the country the user selected.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.