0
1
00:00:00,330 --> 00:00:07,830
So, now that we've managed to safely convert whatever text is inside our displayLabel to a Double and
1

2
00:00:07,830 --> 00:00:15,150
store it inside this constant number, then the next step is to use that number and perform the calculation
2

3
00:00:15,480 --> 00:00:22,350
that triggered this calcButtonPressed, so we can figure out the calculation method by tapping into
3

4
00:00:22,410 --> 00:00:26,640
the sender.currentTitle.
4

5
00:00:27,130 --> 00:00:33,890
So this will be the string that corresponds to whichever of these buttons were pressed.
5

6
00:00:34,090 --> 00:00:37,620
And remember again, currentTitle is an optional string,
6

7
00:00:37,780 --> 00:00:46,660
so we're going to use optional binding, again, to say, if let calc Method = sender.current title,
7

8
00:00:47,260 --> 00:00:56,680
then we're going to say if the calcMethod is equal to the text of the button that is "+/-"
8

9
00:00:56,860 --> 00:01:01,840
then in that case, we want to flip the sign of the number.
9

10
00:01:01,840 --> 00:01:10,780
So we're going to set the displayLabel.text to equal on number that we've converted over here, and
10

11
00:01:10,780 --> 00:01:14,240
we're going to multiply it by minus 1.
11

12
00:01:14,380 --> 00:01:19,840
So that means that if the number that we entered was 8 then when we press this button it should turn
12

13
00:01:19,840 --> 00:01:24,730
it into minus 8 by multiplying it by minus 1.
13

14
00:01:24,730 --> 00:01:31,030
Now, the math's workout, but we have an error here that says, "Cannot assign value of type "Double to type
14

15
00:01:31,030 --> 00:01:31,640
'String?'
15

16
00:01:31,750 --> 00:01:37,150
So this right-hand side of the equation is going to be a Double because it's a Double multiplied by
16

17
00:01:37,150 --> 00:01:37,820
a Double,
17

18
00:01:38,110 --> 00:01:46,590
and the left-hand side, however, is expecting a string-- is expecting a string because it's the text for
18

19
00:01:46,590 --> 00:01:48,060
the displayLabel.
19

20
00:01:48,060 --> 00:01:53,820
So in this case, we're going to have to cast this Double into a String, and as we mentioned before,
20

21
00:01:53,910 --> 00:01:57,030
this never fails. You can always turn a number into a String.
21

22
00:01:57,270 --> 00:02:05,820
So, now if we run our app and test it, you can see that if I type a number, say, 89, and I press this button,
22

23
00:02:05,940 --> 00:02:11,300
then it becomes minus 89. And if I press it again, it becomes plus 89 again.
23

24
00:02:11,580 --> 00:02:15,280
So, now that we've got this working, here's a challenge for you.
24

25
00:02:15,480 --> 00:02:18,630
I want to make these other two buttons work.
25

26
00:02:18,630 --> 00:02:24,510
Remember that whenever I press the AC button, it should revert the display back to zero.
26

27
00:02:24,840 --> 00:02:31,140
And whenever I press the percentage button, it should take the current value and divide it by 100.
27

28
00:02:31,140 --> 00:02:35,640
So once you're ready, pause the video and try to complete that challenge.
28

29
00:02:36,960 --> 00:02:42,780
Okay. So, now that we've really got one of these methods established, it's not so hard to write out the code
29

30
00:02:42,780 --> 00:02:51,910
for the other ones. So let's add an "else if" and say if the calcMethod is, instead, equal to "AC" then that
30

31
00:02:51,910 --> 00:02:58,840
means that we actually want our displaLabel.text to simply just read "0."
31

32
00:02:59,020 --> 00:03:05,440
So we're going to add a string that is "0" and that already sorts out that particular case.
32

33
00:03:05,440 --> 00:03:13,750
Now, finally, if we have another calcMethod that was equal to the percentage sign, then in this case, we
33

34
00:03:13,750 --> 00:03:19,420
simply want to divide the number by 100, or alternatively, if you wish,
34

35
00:03:19,420 --> 00:03:23,050
you can also multiply it by  0.01, it does the same thing.
35

36
00:03:23,410 --> 00:03:31,560
So, in this case, we're going to set the displayLabel.text =  number * 0.01,
36

37
00:03:31,600 --> 00:03:36,070
and we, of course, have to turn this Double into a String.
37

38
00:03:36,070 --> 00:03:42,760
So, now let's run our app and let's say, 89, as a percentage is 0.89,
38

39
00:03:42,880 --> 00:03:49,900
and then if we turn it into a negative and we press the AC button to clear our screen, and you can see everything
39

40
00:03:50,050 --> 00:03:51,310
is working.
40

41
00:03:51,460 --> 00:03:55,490
So, now we sorted out these top three buttons.
41

42
00:03:55,510 --> 00:03:59,620
Now, there's just one slight problem.
42

43
00:03:59,620 --> 00:04:02,400
You see we've got this decimal point here.
43

44
00:04:02,470 --> 00:04:08,390
And if I wanted to write 2.3, then this becomes a double
44

45
00:04:08,410 --> 00:04:10,160
that is 2.3.
45

46
00:04:10,460 --> 00:04:18,610
But I can also press this button again and I've got 2.3.6 which is not a mathematical
46

47
00:04:18,610 --> 00:04:19,140
number.
47

48
00:04:19,150 --> 00:04:20,970
I don't even know what that is.
48

49
00:04:20,980 --> 00:04:24,000
So we have to ensure that this doesn't happen.
49

50
00:04:24,100 --> 00:04:26,900
And that is what we're going to address in the next lesson.
