So, now that we've managed to safely convert whatever text is inside our displayLabel to a Double and 1
store it inside this constant number, then the next step is to use that number and perform the calculation 2
that triggered this calcButtonPressed, so we can figure out the calculation method by tapping into 3
the sender.currentTitle. 4
So this will be the string that corresponds to whichever of these buttons were pressed. 5
And remember again, currentTitle is an optional string, 6
so we're going to use optional binding, again, to say, if let calc Method = sender.current title, 7
then we're going to say if the calcMethod is equal to the text of the button that is "+/-" 8
then in that case, we want to flip the sign of the number. 9
So we're going to set the displayLabel.text to equal on number that we've converted over here, and 10
we're going to multiply it by minus 1. 11
So that means that if the number that we entered was 8 then when we press this button it should turn 12
it into minus 8 by multiplying it by minus 1. 13
Now, the math's workout, but we have an error here that says, "Cannot assign value of type "Double to type 14
'String?' 15
So this right-hand side of the equation is going to be a Double because it's a Double multiplied by 16
a Double, 17
and the left-hand side, however, is expecting a string-- is expecting a string because it's the text for 18
the displayLabel. 19
So in this case, we're going to have to cast this Double into a String, and as we mentioned before, 20
this never fails. You can always turn a number into a String. 21
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
then it becomes minus 89. And if I press it again, it becomes plus 89 again. 23
So, now that we've got this working, here's a challenge for you. 24
I want to make these other two buttons work. 25
Remember that whenever I press the AC button, it should revert the display back to zero. 26
And whenever I press the percentage button, it should take the current value and divide it by 100. 27
So once you're ready, pause the video and try to complete that challenge. 28
Okay. So, now that we've really got one of these methods established, it's not so hard to write out the code 29
for the other ones. So let's add an "else if" and say if the calcMethod is, instead, equal to "AC" then that 30
means that we actually want our displaLabel.text to simply just read "0." 31
So we're going to add a string that is "0" and that already sorts out that particular case. 32
Now, finally, if we have another calcMethod that was equal to the percentage sign, then in this case, we 33
simply want to divide the number by 100, or alternatively, if you wish, 34
you can also multiply it by 0.01, it does the same thing. 35
So, in this case, we're going to set the displayLabel.text = number * 0.01, 36
and we, of course, have to turn this Double into a String. 37
So, now let's run our app and let's say, 89, as a percentage is 0.89, 38
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
is working. 40
So, now we sorted out these top three buttons. 41
Now, there's just one slight problem. 42
You see we've got this decimal point here. 43
And if I wanted to write 2.3, then this becomes a double 44
that is 2.3. 45
But I can also press this button again and I've got 2.3.6 which is not a mathematical 46
number. 47
I don't even know what that is. 48
So we have to ensure that this doesn't happen. 49
And that is what we're going to address in the next lesson.