Day 38 - 100 Days of Swift

3 minute read

Project 8 (part 3)

Day 38 is the third part of the eighth project. You review the stuff that you’ve learned in this project and then he gives you three challenges to extend the app. He challenges you to draw a gray line around buttonsView to offset it from the rest of the UI a little bit. He challenges you to show the user an alert when they submit an incorrect answer. And he challenges you to refactor a little bit to allow for decreasing the score when the user makes an incorrect guess.

Adding the border was pretty simple. I added these three lines in loadView where buttonsView is created:

1
2
3
buttonsView.layer.borderColor = UIColor.lightGray.cgColor
buttonsView.layer.borderWidth = 2
buttonsView.layer.cornerRadius = 12

For presenting the alert, I refactored a little bit and gave myself a helper function for presenting alerts, since I would be writing almost the exact same code as presenting the end of level alert:

1
2
3
4
5
6
7
8
9
10
11
12
13
func presentAlert(title: String,
                  message: String,
                  buttonTitle: String,
                  handler: @escaping (UIAlertAction) -> Void = { _ in }) {
    let alertController = UIAlertController(title: title,
                                            message: message,
                                            preferredStyle: .alert)
    let action = UIAlertAction(title: buttonTitle,
                               style: .default,
                               handler: handler)
    alertController.addAction(action)
    present(alertController, animated: true)
}

Then, I changed presenting the end of level alert to look like this:

1
2
3
4
presentAlert(title: "Well done!",
             message: "Are you ready for the next level?",
             buttonTitle: "Let's go!",
             handler: levelUp)

And I added this in the else block of the check for the solution’s index, since that is the check that will tell us if the user’s input is in the array of solutions or not:

1
2
3
4
5
6
guard let solutionPosition = solutions.firstIndex(of: answerText) else {
    presentAlert(title: "Wrong answer!",
                 message: "Sorry, that isn't a valid word.",
                 buttonTitle: "Ok")
    return
}

In order to decrement the score when the user submits an incorrect answer, I first needed to change how we evaluated the end condition. Right now we are checking if the score is evenly divisible by 7, which works as long as you only add one point for each correct word, but falls apart if you start taking points away. There are many ways to solve this, but I realized that we have an array of solutions already, and there is one point in the code where we have an array of the answers so far, so if we just compare the two and they are equal, then the user has found all the answers. I added this line after adding the new answer into splitAnswer:

1
let isLevelOver = splitAnswers == solutions

And changed my if statement to:

1
if isLevelOver {

Then I just needed to add a decrement to score before presenting the wrong answer alert:

1
score -= 1

With that done, it looks like this:

Screenshot of working app submitting wrong answer.
Screenshot of working app showing wrong answer alert.

You can find my version of this project at the end of day 38 on Github here.