Group Group Group Group Group Group Group Group Group

Part 8: A New Algorithm


#1

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

A New Algorithm Challenge Solution

You should have arrived at something like this:

var difference = currentValue - targetValue
if difference < 0 {
  difference = difference * -1
}

This is a pretty straightforward translation of the new algorithm.

You first subtract the two variables and put the result into the difference variable.

Notice that you can create the new variable and assign the result of a calculation to it, all in one line. You don’t need to put it onto two different lines, like so:

var difference: Int
difference = currentValue - targetValue

Also, in the one-liner version you didn’t have to tell the compiler that difference takes Int values. Because both currentValue and targetValue are Ints, Swift is smart enough to figure out that difference should also be an Int.

This feature is called type inference and it’s one of the big selling points of Swift.

Once you have the subtraction result, you use an if statement to determine whether difference is negative, i.e. less than zero. If it is, you multiply by -1 and put the new result – now a positive number – back into the difference variable.

When you write,

difference = difference * -1

the computer first multiplies difference’s value by -1. Then it puts the result of that calculation back into difference. In effect, this overwrites difference’s old contents (the negative number) with the positive number.

Because this is a common thing to do, there is a handy shortcut:

difference *= -1

The *= operator combines * and = into a single operation. The end result is the same: the variable’s old value is gone and it now contains the result of the multiplication.

You could also have written this algorithm as follows:

var difference = currentValue - targetValue
if difference < 0 {
  difference = -difference
}

Instead of multiplying by -1, you now use the negation operator to ensure difference’s value is always positive. This works because negating a negative number makes it positive again. (Ask a math professor if you don’t believe me.)

Tracking Rounds Challenge Solution

If you guessed that you had to add another instance variable, then you were right. You should have added the following line (or something similar) to ViewController.swift:

var round = 0

It’s also OK if you included the name of the data type, even though that is not strictly necessary:

var round: Int = 0

Also add an outlet for the label:

@IBOutlet weak var roundLabel: UILabel!

As before, you should connect the label to this outlet in Interface Builder.

Don’t forget to make those connections

Forgetting to make the connections in Interface Builder is an often-made mistake, especially by yours truly.

It happens to me all the time that I make the outlet for a button and write the code to deal with taps on that button, but when I run the app it doesn’t work. Usually it takes me a few minutes and some head scratching to realize that I forgot to connect the button to the outlet or the action method.

You can tap on the button all you want, but unless that connection exists your code will not respond.

Finally, updateLabels() should be modified like this:

func updateLabels() {
  targetLabel.text = String(targetValue)
  scoreLabel.text = String(score)
  roundLabel.text = String(round)    // add this line
}

Did you also figure out where to increment the round variable?

I’d say the startNewRound() method is a pretty good place. After all, you call this method whenever you start a new round. It makes sense to increment the round counter there.

➤ Change startNewRound() to:

func startNewRound() {
  round += 1           // add this line
  targetValue = ...
}

Note that when you declared the round instance variable, you gave it a default value of 0. Therefore, when the app starts up, round is initially 0. When you call startNewRound() for the very first time, it adds 1 to this initial value and as a result, the first round is properly counted as round 1.

➤ Run the app and try it out. The round counter should update whenever you press the Hit Me! button.

Downloads

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

BullsEye-part7-final-v2.zip (29.1 KB)

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

BullsEye-part8-final-v2.zip (29.1 KB)