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