0
1
00:00:00,500 --> 00:00:09,320
So in the last lesson, we've incorporated a progress bar into our design and we've figured out how we
1

2
00:00:09,320 --> 00:00:17,510
can change the amount of progress that shows by updating a number that, essentially, it represents a percentage
2

3
00:00:17,510 --> 00:00:23,420
from zero to 100. And that number has a data type of a Float,
3

4
00:00:23,420 --> 00:00:28,580
so it's a floating point. And one 1.0 is completion,
4

5
00:00:28,580 --> 00:00:32,780
so 100 percent. 0.0 is the starting point,
5

6
00:00:32,810 --> 00:00:35,120
so it's zero percent.
6

7
00:00:35,150 --> 00:00:42,230
Now, we know that we've managed to get the seconds remaining variable to reduce itself by 1 every time.
7

8
00:00:42,230 --> 00:00:48,560
But we're still a little bit far from what we want which is for the progressBar to show the amount
8

9
00:00:48,650 --> 00:00:54,900
of time that's left. So in order to solve this we have to think about it logically
9

10
00:00:54,910 --> 00:00:57,200
and so here's what we know so far.
10

11
00:00:57,460 --> 00:01:02,850
We're going to need a percentage that's expressed as a floating point or decimal number.
11

12
00:01:03,220 --> 00:01:07,380
So, for example, 56 percent would be naught .56,
12

13
00:01:07,900 --> 00:01:15,100
And we need that percentage to increase from 0.0 to 1.0 representing how far
13

14
00:01:15,100 --> 00:01:17,080
along our egg is,
14

15
00:01:17,080 --> 00:01:24,820
so from just put into the pan all the way to finished boiling, at least for the chosen hardness.
15

16
00:01:24,820 --> 00:01:29,920
Now, the next thing we know is that we have a different amount of time to count down depending on which
16

17
00:01:30,040 --> 00:01:31,870
egg hardness was selected.
17

18
00:01:31,990 --> 00:01:37,310
So soft eggs require a total of 300 seconds, hard eggs require 720.
18

19
00:01:37,300 --> 00:01:44,890
Now, we also have a function called updateTime that can do something every second because it gets called
19

20
00:01:44,920 --> 00:01:51,850
by our timer which ticks every second and triggers update time.
20

21
00:01:51,850 --> 00:01:58,600
So at the moment, we've got this variable code Seconds Remaining which basically shrinks by one every
21

22
00:01:58,600 --> 00:02:05,690
second and we're printing it out, but we can't really use it directly in our progress bar.
22

23
00:02:05,710 --> 00:02:08,870
So what if we thought about it the other way?
23

24
00:02:09,040 --> 00:02:15,940
What if instead of having a second remaining, we had something called Seconds Passed, and we look at how
24

25
00:02:15,940 --> 00:02:23,240
Seconds Passed varies compared to the total amount of time that the particular egg hardness requires.
25

26
00:02:23,260 --> 00:02:31,270
So then we can track our progress from zero to 90 depending on how many Seconds Passed out of the Total
26

27
00:02:31,270 --> 00:02:32,170
Time.
27

28
00:02:32,200 --> 00:02:35,130
So effectively, we can express this as an equation.
28

29
00:02:35,140 --> 00:02:40,750
We could say that the percentageProgress is equal to the seconds that have passed, divided by the total
29

30
00:02:40,750 --> 00:02:49,770
amount of time that the egg requires. Applying that logic to our code then involves a couple of things.
30

31
00:02:49,810 --> 00:02:53,460
Firstly, I'm going to delete this line of code because we don't need it.
31

32
00:02:53,500 --> 00:02:57,640
We're not updating the progressBar in the IBAction.
32

33
00:02:57,970 --> 00:03:04,630
And the second thing is that notice I've still got these quick and easy-to-test seconds 3, 4, and
33

34
00:03:04,630 --> 00:03:11,110
7, and I'm not going to change this until the very end when we are actually ready to wait 5 minutes
34

35
00:03:11,110 --> 00:03:12,970
for our egg to be done.
35

36
00:03:12,970 --> 00:03:15,880
So at the moment, we have all secondsRemaining
36

37
00:03:15,970 --> 00:03:22,480
and I'm actually going to delete this variable and replace it with two others.
37

38
00:03:22,480 --> 00:03:30,450
One is totalTime, which I'll set to zero, and another is the secondsPassed.
38

39
00:03:30,490 --> 00:03:35,950
So this secondsPassed, unlike secondsRemaining, is actually going to go up. So it's gonna go from zero
39

40
00:03:35,950 --> 00:03:41,340
to 1 to 2, whereas secondsRemaining counted down from the total amount of time.
40

41
00:03:41,530 --> 00:03:47,740
So the place where we need to set the total amount of time for our egg is, of course, here.
41

42
00:03:48,580 --> 00:03:54,490
So instead of setting secondsRemaining, the total amount of time is going to be based on the hardness
42

43
00:03:54,520 --> 00:03:55,660
that's selected,
43

44
00:03:55,660 --> 00:04:04,300
so it's either going to be 3, 4, or 7. And then we need to have this secondsPassed variable increase
44

45
00:04:04,330 --> 00:04:06,940
by 1 every single second.
45

46
00:04:06,940 --> 00:04:10,830
So instead of minus equals, we changed that to plus equals.
46

47
00:04:10,840 --> 00:04:15,800
So now secondsPassed is gonna go from zero to 1 to 
2 every time
47

48
00:04:15,820 --> 00:04:20,900
updateTimer is called which is every 1 second.
48

49
00:04:20,980 --> 00:04:27,340
So now I'm going to delete this print statement which uses that old secondsRemaining, and instead of
49

50
00:04:27,340 --> 00:04:30,460
checking for whether secondsRemaining is greater than zero,
50

51
00:04:30,850 --> 00:04:38,620
I'm going to check to see if the secondsPassed is less than the total amount of time.
51

52
00:04:38,740 --> 00:04:44,130
So let's say that we selected the Soft egg in our button selection.
52

53
00:04:44,200 --> 00:04:52,450
So the total amount of time gets set to 3 and the first time updateTimer gets called, secondPassed
53

54
00:04:52,570 --> 00:04:53,450
is zero,
54

55
00:04:53,470 --> 00:04:55,270
totalTime is 3..
55

56
00:04:55,270 --> 00:05:02,230
So zero is less than 3 and, therefore, secondsPassed gets increased by 1, and then the next time, and
56

57
00:05:02,230 --> 00:05:08,500
then the next time, until secondsPassed is equal to 3. At which point, it's no longer less than totalTime,
57

58
00:05:08,500 --> 00:05:11,890
At which point, our egg is done.
58

59
00:05:11,890 --> 00:05:18,950
So this "if" statement is the place where we're going to update our percentage progress.
59

60
00:05:19,000 --> 00:05:27,340
So just before we increase it, we're going to create a new constant code percentageProgress and we're
60

61
00:05:27,340 --> 00:05:38,300
going to set it equal to the secondsPassed divided by the total amount of time. And so now we have this
61

62
00:05:38,300 --> 00:05:47,620
thing called percentageProgress and we're going to set that as the progressBar's progress property.
62

63
00:05:47,870 --> 00:05:51,770
So we'll set that equal to percentageProgress.
63

64
00:05:51,770 --> 00:05:59,210
Now, we're going to have an error there because it says that the percentage progress has a data type
64

65
00:05:59,360 --> 00:06:00,710
of a whole number,
65

66
00:06:00,980 --> 00:06:05,900
but the progressBar.progress needs a floating point number.
66

67
00:06:06,100 --> 00:06:12,350
And we can verify this by, again, holding down option and clicking on that percentageProgress is, in fact,
67

68
00:06:12,470 --> 00:06:13,730
an integer.
68

69
00:06:13,730 --> 00:06:18,860
So how can we convert an integer into a float?
69

70
00:06:18,860 --> 00:06:25,940
Well, Xcode seems to know. It says, "Well, why don't you wrap your percentageProgress inside a float?"
70

71
00:06:26,810 --> 00:06:31,970
So let's try it and you'll see that Xcode doesn't always know what it's doing.
71

72
00:06:32,390 --> 00:06:43,140
So if we click fix and we try to run our code and we press on a button, you can see that it goes straight
72

73
00:06:43,140 --> 00:06:44,810
down to zero.
73

74
00:06:44,820 --> 00:06:52,140
In fact, our progressBar doesn't do anything other than just make the progress go down to zero.
74

75
00:06:52,590 --> 00:06:54,150
So what's going on here?
75

76
00:06:54,540 --> 00:07:02,220
Well, this is a bug in our code. And like the olden days where the computers would get a moth stuck in
76

77
00:07:02,220 --> 00:07:08,100
it and some bit of circuit won't work, in modern days, we don't have to worry about moths so much, but
77

78
00:07:08,100 --> 00:07:09,940
we do have to worry about ourselves.
78

79
00:07:09,960 --> 00:07:17,640
We are our own greatest enemies and are fallacies and logic leads to our code-breaking and not working.
79

80
00:07:18,360 --> 00:07:19,580
In the next lesson,
80

81
00:07:19,590 --> 00:07:22,940
we've got a five-step process.
81

82
00:07:22,980 --> 00:07:24,360
Again, five steps.
82

83
00:07:24,450 --> 00:07:26,230
I really like the number five.
83

84
00:07:27,000 --> 00:07:33,850
And it's a five-step process for you to figure out how to debug any problem you come across.
84

85
00:07:33,900 --> 00:07:37,140
So join me on the next lesson where we're going to fix this.
