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

Stuck on Project 2 challenge questions of the UIkit version

Forums > 100 Days of Swift

Feeling depressed and frustrated if I am being honest. I understood every single concept along the way and practiced along in playground but on the second project, I don't know where to start with challenge questions. Do I need to revisit the material from the begining? I even redid project 2 like 5 times to understand and I understand what's being done but when I try to do the challenge questions, it's like I don't know where to start. It's been 5 days now and I vowed not to look at any solutions until I figure it out myself but I'd like to hear some insights.


Its called "Coder's Black Hole", no need to feel defeated, just move on, we all go through it ..


I know if I move on, I am going to find myself doing the same exact thing on the next project. Any other takers?


@Geel is frustrated:

I don't know where to start with challenge questions.

I didn't follow the UIKit path. I had tried UI Kit via other tutorials (CS193p, and Angela Yu's courses). I found them frustrating too.

You don't say which challenge question you're struggling with, there are three.

Break your big problem into several smaller, solvable problems.

But perhaps you can step away from the code for a bit, and first solve this on paper.

Challenge #2
Keep track of how many questions have been asked,
and show one final alert controller after they have answered 10.
This should show their final score.

Solve this on paper before trying to code this.

  1. Keep track of how many questions....

You can't have 1.45 questions, or "grok" questions. So you know you'll be keeping track of questions via an integer. What's a good name for an integer to track number of questions asked? questionCount ?
You also have to keep track of the game's state. Is the game over or not? Consider creating a computed var named isGameOver and make it a boolean. On paper, consider how you would calculate the boolean isGameOver each time a player taps a flag.

Do you have a function in your program that generates a question?
That might be a great place to increment your questionCount variable.

Do you have a function that runs when a player makes a selection?
That might be a great place to ask the isGameOver variable to recompute itself. Is the game over or not?

  1. After they answer 10 questions.....

If the isGameOver computed var returns true, what course of action comes next? You want to show a new view! You know how to do this. Only show the new view if isGameOver is true.


  1. Try showing the player’s score in the navigation bar, alongside the flag to guess.

This one is pretty simple. There is currently a line of code in your project that you used to set the title of the navigation bar to show the name of a country...

title = countries[correctAnswer].uppercased()

Can you modify that line of code to show the score as well?

  1. Keep track of how many questions have been asked, and show one final alert controller after they have answered 10. This should show their final score.

This one is a little more complicated. But, you have been shown some examples of how these things can be done.

First you just have to create a variable to hold an integer value for the currentQuestion. That part should be easy.

Then you have to make sure that currentQuestion is incremented every time a new question gets asked. So, where would be the best place to add that into your code? Where is the place where a new question is being asked, or about to be asked?

But now, we only want currentQuestion to be incremented and our new question to be asked if we have not answered 10 questions already. So, how can we make it so that this code will only run if a certain condition is met?

Otherwise (else), we want to show an alert controller saying that the game has ended.

You have seen how to create and show an alert controller already...

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)

Now you just have to make your own. The first line in this example creates an alert controller with the title and message that we want it to show. The second line adds a button to the alert controller. The third line makes the alert controller show on the screen.

  1. When someone chooses the wrong flag, tell them their mistake in your alert message – something like “Wrong! That’s the flag of France,” for example.

After you have completed the first two challenges, this one should be a little bit easier to figure out. It's kind of a combination of the concepts we used in the first two challenges in a way.

We just need to find the code for the alert controller that currently shows when the user taps one of the flag buttons, and change the message string to include a variable in it like we did in challenge one. But, we want to show one message when the answer is wrong, and another when the answer is correct, so, we will probably need to use if/else again like we did in challenge 2.

The tricky part is figuring out where to put the if/else logic here. But look at this line of code for creating an alert controller again carefully...

let ac = UIAlertController(title: title, message: "Your score is \(score).", preferredStyle: .alert)

Notice how we are using a variable for the title here, but typing a String in place for the message. Maybe it could be helpful to use a variable for the message as well. (You could name it something like alertMessage. Then, we might be able to get the alertMessage variable set properly before we create and display the AlertController like we did with the title earlier in the project.


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.