Now, in the last lesson, we sorted out our little decimal point bug there. So we're no longer able to write 1
8.2.3.5, et cetera. 2
And we can only add one decimal point to make our integer into a Double. 3
So in this lesson, though, I want to look at how we can improve our current code because you can see that 4
it's getting a little bit too long. 5
Now, whenever you find that your files getting a bit too long, a neat trick is you can collapse individual 6
methods and individual blocks of code. 7
So, for example, if I wanted to collapse this IBAction numButtonPressed, then I can click just under 8
the first line. I can hold down command option, and if I press the left button, then you can see that 9
Xcode will neatly fold away 10
that method, saving me my sanity and also clearing up the screen. 11
And whenever I want to expand it, I can either double click on here or hold down option command, and hit 12
the right key. 13
Now, in our case, though, we can actually do one step better than that. Instead of just stashing everything 14
away and hiding it by folding up our methods, 15
we can actually refactor it and make it a lot simpler. 16
And the thing that pains me right now is that we're converting numbers into Strings, and then Strings 17
into numbers all over the place. 18
And this isn't ideal use case for computed properties. 19
So we learn about computed properties in the last module. 20
Let's see if we can use what we learned to simplify our code and reduce redundancy. 21
So right at the top here, just below the private var, 22
I'm going to create a new private var, and remember what we said in the access levels lesson, 23
whenever you're creating a global variable or something that is class-wide, you should always add the 24
"private" keyword in front of it, so that it limits the scope of this variable to only the enclosing curly 25
braces. 26
So this private var, I'm going to call displayValue, and this is going to be a Double and it's also going 27
to be a computed property. 28
So it's going to have a getter and a setter. 29
So as a challenge, I want you to create a getter that simply gets the current value from the 30
displayLabel.text. 31
So pause the video and see if you can complete this challenge. 32
So if you remember, to create a getter, we just have to use the keyword "get," and then we open up a set 33
of curly braces. 34
Now, inside here, we have to return a value and that is going to be the value that we're going to give 35
whenever somebody tries to access displayValue. 36
Now, currently, whenever we need the value that's inside the displayLabel, we just say displayLabel.text, 37
force unwrapped, 38
and then we turn it into a Double. So we can do that over here as well. 39
Instead of writing it all out here, we can copy it into our getter and we can return this number. 40
So that means that we don't have to have two guard statements or more if we decide to continue our code. 41
And instead, we can simply delete these lines of code and replace the places where it was used with 42
displayValue. 43
So, now when we hit this line of code and we're trying to figure out what to set inside the displayLabel, 44
then it's going to look and try and fetch the currentValue of the displayLabel which triggers our 45
get block, and it's going to try and convert what's currently inside the displayLabel.text into 46
a Double. 47
And if it fails, then it'll throw a fatalError. 48
But if it succeeds, then it's going to return that number as the value of our displayValue. 49
So we can also delete these three lines of code here as well, 50
and we can change this into our displayValue, and this to our displayValue. 51
So we've now really saved ourselves six lines of code. 52
Now, the next thing I want to do is I want to create a setter for our displayValue and this is going 53
to be a challenge for you as well. 54
And so I want you to create a setterr that sets the displayLabel whenever the displayValue is updated. 55
Because you can see over here, we're doing a lot of that setting manually and repeatedly. We're taking 56
the displayValue, we're performing the calculation on it, then we're changing it back into a String, and 57
then we're putting it into the displayLabel. 58
I want to simplify all of this by simply adding a setter to our displayValue computed property. 59
So, pause the video and see if you can complete the challenge. 60
All right. 61
So to create setters, we simply write a set. And now, in here, we can tap into the exact moment where our 62
displayValue is updated. 63
And when that does happen, we can say displayLabel.text is equal to the newValue, and we can convert 64
the newValue which is going to be a Double because displayValue is a Double, 65
then we can convert that Double into a String and put it into our displayLabel.text. 66
So, now instead of having this line of code, we can simply say displayValue = displayValue 67
multiplied by -1, or if you really want to be fancy, you can, of course, do multiply equals 68
minus 1. 69
So displayValue = displayValue *= -1 depending on which syntax you find easier 70
to understand, then just use whichever one suits you. 71
Now, I can also update it over here by saying displayValue *= 0.01, 72
and we've now simplified another two lines of code, 73
all because we now know how to use computed properties. 74
So, as you can see, by learning all of these advanced concepts, we're not doing anything that we weren't 75
able to do before, 76
but we're simply making our code more manageable and reducing the complexity, and therefore, the places 77
where we can potentially make errors. 78
So that means we can make more complex apps and write more code without getting it muddled into a bowl 79
of spaghetti.