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