0
1
00:00:00,330 --> 00:00:05,880
So as we mentioned in the last lesson, our calculator has a bit of a logical issue.
1

2
00:00:05,880 --> 00:00:09,510
It lets us add as many decimal points as we want
2

3
00:00:09,750 --> 00:00:18,230
and this will dramatically screw up our app because when we try to, say, multiply this by minus 1
3

4
00:00:18,290 --> 00:00:20,430
or we try to turn it into a percentage,
4

5
00:00:20,430 --> 00:00:27,750
we have a fatal error and the error is, of course, cannot convert display labeled text to a Double because
5

6
00:00:28,080 --> 00:00:33,280
I'm not even sure what 2.3.6.23 is as a number.
6

7
00:00:33,330 --> 00:00:35,760
It's definitely not a decimal number.
7

8
00:00:35,760 --> 00:00:37,710
All right, so what can we do instead?
8

9
00:00:37,920 --> 00:00:44,760
Well, down here where we've got our numButtonPressed, we should be able to check to see if we currently
9

10
00:00:44,760 --> 00:00:46,700
have an integer in here.
10

11
00:00:46,860 --> 00:00:55,680
So, say, if I had an 8 in here, then I should be able to use the .button, but if I already have a Double,
11

12
00:00:55,710 --> 00:01:02,390
say, 8.5, then I shouldn't be able to press this button or add that dot to the display any
12

13
00:01:02,400 --> 00:01:03,110
more.
13

14
00:01:03,270 --> 00:01:09,660
So in order to do that, we're going to tap into the else statement inside the numButtonPressed.
14

15
00:01:09,660 --> 00:01:17,180
Because when isFinishedTypingNumber is true, then the current value in here will be 0.
15

16
00:01:17,190 --> 00:01:23,520
It's only when we've got an actual number in here and we switched isFinishedTypingNumber to false
16

17
00:01:23,940 --> 00:01:32,610
where we actually want to check if the current number value that's being typed in is equal to a full
17

18
00:01:32,610 --> 00:01:35,530
stop, namely a decimal point.
18

19
00:01:35,700 --> 00:01:40,940
And in that case, then we can say that let isInt,
19

20
00:01:41,010 --> 00:01:47,760
so we're going to create a new local constant equal to floor.
20

21
00:01:47,760 --> 00:01:53,320
So we're going to round down whatever is currently inside the displayLabel.
21

22
00:01:53,490 --> 00:01:55,650
So it's expecting a Double,
22

23
00:01:55,650 --> 00:02:04,390
so I have to convert our displayLabel to a Double, then I'm going to say displayLabel.text.
23

24
00:02:04,390 --> 00:02:11,670
And now, I've got whatever it is that's currently inside my displayLabel rounded down to the floor.
24

25
00:02:11,720 --> 00:02:16,940
So, say, if I had 8.1 in here, then it would round it down to 8.
25

26
00:02:17,090 --> 00:02:19,010
So, now I want to be able to--
26

27
00:02:19,100 --> 00:02:28,050
So, now I want to be up to compare this with the non-rounded down version of my displayLabel.text.
27

28
00:02:28,100 --> 00:02:35,300
Now, just as we saw previously, I can either force unwrap the Double initialization to say that this will
28

29
00:02:35,360 --> 00:02:44,540
always be convertible to a Double, or what I want to do is to write a "guard" statement that checks whether
29

30
00:02:44,540 --> 00:02:53,090
if this is, in fact, convertible to a Double, and also to add a fatal error and a message if it is not.
30

31
00:02:53,180 --> 00:02:54,770
So same as what we did before.
31

32
00:02:54,770 --> 00:02:57,750
Pause the video and see if you can complete this challenge.
32

33
00:02:59,100 --> 00:02:59,360
All right.
33

34
00:02:59,370 --> 00:03:00,740
So this is not so hard.
34

35
00:03:00,780 --> 00:03:09,180
We're just going to write a "guard let," and we're going to say currentDisplayValue = 
35

36
00:03:09,180 --> 00:03:09,860
displayLabel.text
36

37
00:03:09,870 --> 00:03:20,620
cast into a double. Now, if this is not true, i.e., fails, we get a nil,
37

38
00:03:20,620 --> 00:03:29,170
then we want to throw a fatal error where we say "Cannot convert display label text to a Double!"
38

39
00:03:31,770 --> 00:03:40,040
And now instead of using this number, we can simply use the currentDisplayalue over there and over
39

40
00:03:40,040 --> 00:03:41,070
here.
40

41
00:03:41,600 --> 00:03:49,970
So, now our code looks a little bit simpler and all it does is it says round down the
41

42
00:03:49,970 --> 00:03:50,810
currentDisplayValue.
42

43
00:03:50,810 --> 00:03:56,640
So if it's 8.1, round it down to 8. And that's what the floor method does,
43

44
00:03:56,690 --> 00:03:59,260
it simply rounds our number down.
44

45
00:03:59,270 --> 00:04:07,700
Now, the other part is comparing whether if the rounded down version of our display value is equal to
45

46
00:04:07,700 --> 00:04:09,340
the currentDisplayValue.
46

47
00:04:09,500 --> 00:04:12,720
So is 8.0 equal to 8.1?
47

48
00:04:12,740 --> 00:04:14,320
Well, that's false, right?
48

49
00:04:14,330 --> 00:04:18,420
So then, is Integer becomes false.
49

50
00:04:18,440 --> 00:04:26,730
Now, however, if I had 8 in here instead, then 8 rounded down is equal to 8.
50

51
00:04:26,740 --> 00:04:29,150
So isInt equal to true.
51

52
00:04:29,480 --> 00:04:34,940
And so this means that we can check to see if we don't have an integer,
52

53
00:04:35,120 --> 00:04:39,660
namely, we've got a number that has a decimal place in here,
53

54
00:04:39,900 --> 00:04:42,410
then isInt is false.
54

55
00:04:42,410 --> 00:04:53,390
So if isInt is false, then we're simply going to return. And this keyword forces our function to return,
55

56
00:04:53,390 --> 00:05:01,100
and it means that we don't continue appending the current numValue which, remember, is a full stop to
56

57
00:05:01,190 --> 00:05:02,430
the display.
57

58
00:05:02,840 --> 00:05:06,670
So let's go ahead and run this and see what happens.
58

59
00:05:06,680 --> 00:05:07,550
So let's try
59

60
00:05:07,550 --> 00:05:09,490
8.1.
60

61
00:05:09,770 --> 00:05:15,620
And then I'm going to try add another decimal place to the end of this. And you can see that no matter
61

62
00:05:15,620 --> 00:05:18,530
how hard I press, it's not letting me.
62

63
00:05:18,620 --> 00:05:26,180
But if I clear the screen and it was a single integer number, then I can add a single decimal place.
63

64
00:05:26,420 --> 00:05:29,310
So that's sorted out our little problem there.
