Now, if you manage to complete the challenge from the last lesson, then this should be the effect that 1
you now have. When you press on one of these buttons, 2
so the Soft button, the countdown timer should start from 300, 3
Medium starts from 420, and Hard starts from 720. 4
So now if I press on the Soft button, you'll see that I'm getting a countdown timer that fires every 5
second 6
counting down the amount of time that my egg still has to cook. 7
Now, if you haven't got this effect or you don't know what I'm talking about, then you might have missed 8
out on the last lesson which was a challenge 9
lesson. So be sure to complete the challenge, so that we can actually move on. Now, if you have completed 10
the challenge, then I want to go through the code that I've got and you can compare it to yours as well. 11
So the first thing that we did was we converted our minutes to seconds because we're going to be counting 12
down every second. And then we created a variable code secondsRemaining to keep track of how many 13
seconds there are. 14
And then when the user selected the hardness that they wanted, we assigned the value in our eggTimes 15
dictionary, so be it 300, 420, or 720 to the variable secondsRemaining. 16
So at this point, secondsRemaining now equals one of these values. 17
The next thing we do is we create a Timer. 18
So this timer code you would have found probably from StackOverflow or Googling around, 19
but, essentially, it has a bunch of parameters in it. 20
So it asks you for "How often do you want the timer to fire?" 21
So in our case, because we want our timer to update every second, then we've set that as 1.0, 22
so 1.0 seconds. And then we've got a parameter for whether if we want our timer to repeat, 23
so we do, we don't want it just to stop after the first second. 24
So we've set that as "true." 25
And then we've got this parameter code selector. And the selector is a bit of a remnant from the olden 26
days where we used to create things using objective c. 27
So the way that we use it is we provide a name of a function. 28
So it's almost like it calls this function that we put in here every single second every time the timer 29
fires. 30
Now, when you created this function code updateTimer, you might have gotten an error that has a little white 31
dot in it, and it says that selectors come from Objective-C. So in order to expose this method or make 32
this method work with Objective-C timers, then you have to add that little annotation, the @objc 33
in front. 34
So that's all it is. 35
And we don't really need to understand how the timer works or how a selector works. 36
The goal of the challenge was to get you just comfortable with finding out how to do things through 37
searching and through using the five-step method to just make it do what you wanted to do. 38
And in later lessons as we get better at Swift and as we learn more programming, then these small things 39
will become more and more clear to us. 40
So here we have a function called updateTimer which we know will fire every second. In here, we're 41
checking to make sure that we've still got time on the clock, 42
So seconds remaining is larger than zero. 43
And if so, we're going to print the number of seconds that we currently have remaining, and then we're 44
going to minus one from it, so that the next time our timer fires, it will be printing a second 45
that's one lower from before. 46
So this is, essentially, how we've achieved this timer mechanism. 47
But if you've been testing it carefully, you'll notice that if you start out pressing one button, 48
so the one that should start at 300, and then midway, you change your mind and you press a different button, 49
now our timer seems to have sped up. It's actually running down the seconds, almost two seconds in one 50
go. 51
So it's doubling its speed. 52
And if we press another one, then it's going to speed up even more. 53
It's now three times as fast as before. 54
So what's going on here? 55
Well, the reason is because we create this timer, but we never actually cancel it. 56
We never stop it. 57
So it's the same timer that's running. And every time we press one of the button, we create a new timer 58
and we call this method every second. 59
So now we're calling this method twice a second or three times a second depending on how many times 60
we've pressed this hardness selected IBAction. 61
So how can we prevent this from happening? 62
Well, we could create a variable code timer and set it equal to a Timer. And then down here, we assign 63
that timer variable a new piece of data which is the actual scheduledTimer. But every single time that 64
the button, the Soft, Medium, or Hard buttons get pressed, we actually invalidate our timer. 65
So we stop it and cancel it, so that the next time we create a timer, we actually create a brand new one. 66
So now if we run our app and we press one of the buttons, you can see it's counting down one second every 67
second of the clock. 68
And if I change to a different button, it will still count down once per second, instead of speeding up 69
like before. 70
Now, here's another challenge for you. 71
How can we change the label that's currently on our Main.storyboard? 72
So the one here where it says, "How do you like your eggs?" 73
How can we make it say done once our timer is up? 74
So in order to make it easier to test your code, I recommend changing these eggTimes from 300 seconds 75
where you have to wait a whole 5 minutes back down to 3, and 4, and 7. This way when you're 76
testing it, 77
it's much, much faster. 78
Pause the video and try to see if you can make the code change the title label to make it say "Done" 79
once our timer is up? 80
All right, first things first, we need an outlet to be able to change this label. So right up here, 81
let's control, drag it over, and I'm going to call it just a titleLabel, and hit enter. 82
So now we're actually able to change our title label and we're gonna do it right here. 83
So here we have an "if" statement that checks if the secondsRemaining is greater than zero, 84
then continue taking along. 85
But if the secondsRemaining was, in fact, equal to zero, so not greater than zero, 86
well, then we could capture that with an "else" statement. 87
So we add the "else" statement. 88
And when this happens, we first invalidate our timer, 89
so we run our timer.invalidate again to stop the timer from continuing to tick every second. 90
And plus on top of that, we tap into our titleLabel.text property, 91
and we make it say "DONE!" 92
So now if we run our app and I'm going to select the Soft button and you'll see my timer goes 3,2, 1, DONE! 93
So that's exactly what we wanted. 94
All right. 95
So now that we've got our timer functionality down, the next step is to add a progress bar. 96
So that's what we're gonna do in the next lesson. 97
So I'll see you there.