Part 10: Asynchronous Code

Welcome, this is a thread for collaboration, questions, and feedback on Part 10 of the iOS Apprentice Email course.

Start Over Challenge Solution

First, add a method to ViewController.swift that starts a new game. I suggest you put it near startNewRound() because the two are conceptually related.

➤ Add the new method:

func startNewGame() {
  score = 0
  round = 0

This method resets score and round to zero, and starts a new round as well.

Notice that you set round to 0 here, not to 1. You use 0 because incrementing the value of round is the first thing that startNewRound() does.

If you were to set round to 1, then startNewRound() would add another 1 to it and the first round would actually be labeled round 2.

So, you begin at 0, let startNewRound() add one and everything works great.

(It’s probably easier to figure this out from the code than from my explanation. This should illustrate why we don’t program computers in English.)

You also need an action method to handle taps on the Start Over button. You could write a new method like the following:

@IBAction func startOver() {

But you’ll notice that this method simply calls the previous method you added :] So, why not cut out the middleman? You can simply change the method you added previously to be an action instead, like this:

@IBAction func startNewGame() {
  score = 0
  round = 0

You could follow either of the above approaches since both are valid. Personally, I like to have less code since that means there’s less stuff to maintain (and less of a chance of screwing something up :]). Sometimes, there could also be legitimate reasons for having a seperate action method which calls your own method, but in this particular case, it’s better to keep things simple.

Just to keep things consistent, in viewDidLoad() you should replace the call to startNewRound() with startNewGame(). Because score and round are already 0 when the app starts, it won’t really make any difference to how the app works, but it does make the intention of the source code clearer. (If you wonder if you can call an IBAction method directly instead of hooking it up to an action in the storyboard, yes, you certainly can do so.)

➤ Make this change:

override func viewDidLoad() {
  startNewGame()        // this line changed

Finally, you need to connect the Start Over button to the action method.

➤ Open the storyboard and Control-drag from the Start Over button to View Controller. Let go of the mouse button and pick startNewGame from the popup if you opted to have startNewGame() as the action method. Otherwise, pick the name of your action method .

That connects the button’s Touch Up Inside event to the action you have just defined.

➤ Run the app and play a few rounds. Press Start Over and the game puts you back at square one.

Tip: If you’re losing track of what button or label is connected to what method, you can click on View Controller in the storyboard to see all the connections that you have made so far.

You can either right-click on View Controller to get a popup, or simply view the connections in the Connections inspector. This shows all the connections for the view controller.


Here’s a download for the project to use at the beginning of this email: (29.4 KB)

Here’s a download for the project where it stands at the end of this email: (29.3 KB)

You just throw blocks/closures out there like that without any further explanation? Seriously? :slight_smile: