0
1
00:00:00,720 --> 00:00:08,070
Now, the first step is we're going to get started with a brand-new Blank iOS with playground,
1

2
00:00:08,340 --> 00:00:10,760
and you can call it whatever it is you want.
2

3
00:00:10,770 --> 00:00:18,960
I'm just going to call it ComputedProperties and I'm going to save it on my desktop. So first things
3

4
00:00:18,960 --> 00:00:25,350
first, if you've got manual run for your playground enabled, we're going to change it to Automatically
4

5
00:00:25,350 --> 00:00:29,820
Run because we're going to need it to refresh our values on an ongoing basis.
5

6
00:00:30,090 --> 00:00:36,990
Next, I'm going to delete this first variable, and I now have a blank slate to work with.
6

7
00:00:37,020 --> 00:00:43,920
Now, the first thing I'm going to do is I'm going to create a constant called pizzaInInches and this
7

8
00:00:43,920 --> 00:00:51,660
is going to be of data type integer because, usually, you don't get 15.5-inch pizzas,
8

9
00:00:51,720 --> 00:00:57,000
and I'm going to set it to equal just a medium-sized pizza, a 10-inch pizza.
9

10
00:00:57,000 --> 00:01:03,000
Now, I've been doing a lot of research on the Domino's website in order to figure out how many slices
10

11
00:01:03,270 --> 00:01:07,370
of pizza you get in pizzas of different sizes.
11

12
00:01:07,470 --> 00:01:13,470
So what they say is, apparently, if you take the size of the pizza in inches, say, in this case, 
12

13
00:01:13,470 --> 00:01:17,140
it's 10-inch pizza, and you subtract 4 from 10,
13

14
00:01:17,160 --> 00:01:20,400
you end up with the number of slices of pizza.
14

15
00:01:21,000 --> 00:01:27,280
So the next thing I want to do is I want to create a variable called numberOfSlices.
15

16
00:01:27,420 --> 00:01:34,080
And this, again, is going to be an integer that is equal to the number of slices we have in our pizza.
16

17
00:01:34,080 --> 00:01:38,020
So in this case, if we had a 10-inch pizza, 10 minus 4 is 6.
17

18
00:01:38,040 --> 00:01:42,490
So we have six slices of pizza to go around.
18

19
00:01:42,510 --> 00:01:49,850
Now, at the moment, all my properties are manually set, and these are both what's called stored properties.
19

20
00:01:49,860 --> 00:01:53,490
They just store a value and we can then access the value.
20

21
00:01:53,490 --> 00:02:00,030
So, for example, if I wanted to print numberOfSlices, we can simply say print numberOfSlices, and you can
21

22
00:02:00,030 --> 00:02:05,270
see that we've got the result printed down here in the debug area.
22

23
00:02:05,280 --> 00:02:11,280
Now, if you find that your playgrounds does what mine is doing, where it says "Running ComputedProperties"
23

24
00:02:12,270 --> 00:02:18,900
for eternity, and it doesn't actually have anything in the right-hand pane or down here in the debug
24

25
00:02:19,230 --> 00:02:22,540
and it's just getting stuck, and it's being really, really slow,
25

26
00:02:22,710 --> 00:02:29,880
very easy fix for this is to simply go and open up the right-hand pane over here, and go into the file
26

27
00:02:29,880 --> 00:02:30,680
inspector here,
27

28
00:02:30,690 --> 00:02:36,470
this one, and change the Playground Settings from iOS to macOS.
28

29
00:02:36,510 --> 00:02:43,620
Now, at this point, it'll tell you that UIKit doesn't exist for macOS because it's an iOS specific module,
29

30
00:02:43,800 --> 00:02:49,000
so you can simply change it to foundation, and the foundation module contains all of Swift,
30

31
00:02:49,140 --> 00:02:55,860
ao that allows us to test out our code and run our playgrounds without having it hanging forever.
31

32
00:02:55,860 --> 00:03:00,690
This is a bug that Apple has created and they've been trying to fix it for months and months,
32

33
00:03:00,840 --> 00:03:03,220
but I'm still not seeing a lot of improvement.
33

34
00:03:03,240 --> 00:03:04,850
So that's just a neat trick
34

35
00:03:04,980 --> 00:03:07,570
if this happens to you.
35

36
00:03:08,010 --> 00:03:10,520
So, let's get back on track.
36

37
00:03:10,530 --> 00:03:15,780
So at the moment, our number of slices is a stored property.
37

38
00:03:15,870 --> 00:03:21,800
And in order to change it, we have to tap into it manually and update it.
38

39
00:03:21,840 --> 00:03:26,490
So, now it's four slices, right? But that's not really what we want.
39

40
00:03:26,490 --> 00:03:33,190
We actually wanted to be computed or calculated depending on the pizza in inches.
40

41
00:03:33,330 --> 00:03:39,960
So, for example, if I bought a pizza that is a 14-inch, right, say, a large pizza instead,
41

42
00:03:40,050 --> 00:03:43,630
I want this to automatically update to 10 slices.
42

43
00:03:43,650 --> 00:03:45,110
So how can I do that?
43

44
00:03:45,210 --> 00:03:54,330
Well, we can use computed properties to compute the value of this property on the go as it's dependent's
44

45
00:03:54,450 --> 00:03:55,370
change.
45

46
00:03:55,380 --> 00:04:01,350
So instead of having an equal sign and a value, I'm going to delete that and I'm going to open a set
46

47
00:04:01,410 --> 00:04:08,700
of curly braces, and this allows me to put in some code inside that block to calculate what the value
47

48
00:04:08,730 --> 00:04:10,590
of numberOfSlices should be.
48

49
00:04:10,590 --> 00:04:16,470
So in this case, numberOfSlices should equal pizzInInches - four.
49

50
00:04:16,650 --> 00:04:23,860
And in order to give the value back to this property, we have to use a return.
50

51
00:04:23,910 --> 00:04:31,350
So you can see that this is almost like a function that calculates what the value of this property should
51

52
00:04:31,350 --> 00:04:32,130
be.
52

53
00:04:32,130 --> 00:04:35,430
Now, there's a couple of things to note when you're using computed properties.
53

54
00:04:35,480 --> 00:04:39,910
Firstly is that it has to be a var, it has to be a variable.
54

55
00:04:39,990 --> 00:04:46,770
Because if you have a constant, then its value can't change and it doesn't make sense for it to be a
55

56
00:04:46,770 --> 00:04:48,300
computed property.
56

57
00:04:48,450 --> 00:04:56,170
Now, the second rule is that you have to specify a data type explicitly in order to use computed properties.
57

58
00:04:56,190 --> 00:05:03,540
You can't let Swift infer the data type because it will get very confused when it is trying to calculate
58

59
00:05:03,540 --> 00:05:05,130
what the value should be.
59

60
00:05:05,130 --> 00:05:14,140
So to make it simpler and more explicit, we always specify the data type when we're using computed properties.
60

61
00:05:14,160 --> 00:05:17,420
So, now you can see that as I change this value,
61

62
00:05:17,430 --> 00:05:24,540
let's say we bought a small piece instead, then the value of numbers slices updates dynamically without
62

63
00:05:24,540 --> 00:05:27,840
me having to go into it and manually change it.
63

64
00:05:27,900 --> 00:05:33,330
So you can see if we had a lot of properties that were dependent on other properties, say, things such
64

65
00:05:33,330 --> 00:05:39,480
as an area that's dependent on the width and the height, or something like volume, then we can use computed
65

66
00:05:39,480 --> 00:05:45,870
properties to save ourselves a lot of work and potentially a lot of errors as well.
66

67
00:05:45,870 --> 00:05:54,960
Now, what we've got here is actually more specifically a getter because inside this block of code, we're
67

68
00:05:54,960 --> 00:06:01,490
specifying the computation that should happen when you try to get the value of this property.
68

69
00:06:01,680 --> 00:06:06,690
And we're doing that when we're trying to print it or if we're trying to work with it, say, for example,
69

70
00:06:06,720 --> 00:06:15,930
if I wanted to create a new constant called "a" and it was equal to the number of slices times 2.
70

71
00:06:15,930 --> 00:06:19,800
At this point, we're trying to get the value of numberOfSlices.
71

72
00:06:19,800 --> 00:06:23,700
So at this point, we will tap into our getter,
72

73
00:06:23,940 --> 00:06:26,650
and also at this point when we're trying to print it,
73

74
00:06:26,700 --> 00:06:28,570
we will also tap into the getter.
74

75
00:06:28,590 --> 00:06:35,880
So the getter is the code here that will run in order to get us the value of this property.
75

76
00:06:35,880 --> 00:06:42,240
Now, some of you might have realized that even if we didn't have computed properties, we could just as
76

77
00:06:42,270 --> 00:06:49,860
easily have created a function, say, called calcPizza slices, and this function does something very
77

78
00:06:49,860 --> 00:06:59,160
simple where it simply says the numberOfSlices is equal to pizzaInInches - 4.
78

79
00:06:59,600 --> 00:07:08,970
And this is now just a bog-standard stored property and the number of slices can now be worked out
79

80
00:07:09,330 --> 00:07:12,060
by calling calcPizzaSlices.
80

81
00:07:12,060 --> 00:07:14,440
So this also works, of course,
81

82
00:07:14,460 --> 00:07:20,310
and that's what we've been doing so far, because we haven't yet learned about computed properties.
82

83
00:07:20,310 --> 00:07:26,940
But you can see, firstly, it introduces a lot more lines of code, and so that means potential sources of error
83

84
00:07:27,270 --> 00:07:29,460
and places where things can go wrong.
84

85
00:07:29,550 --> 00:07:35,880
So whenever you find yourself creating methods that don't have an input and don't have an output and
85

86
00:07:35,880 --> 00:07:43,470
all it does is execute a block of code, then consider whether if you can instead use a computed property
86

87
00:07:44,370 --> 00:07:49,770
to do exactly the same thing without the need for creating an extra method.
87

88
00:07:49,770 --> 00:07:56,550
Now, what we've got here in between these two curly braces is a block of code that programmers will call
88

89
00:07:56,730 --> 00:08:05,660
a getter because what it does is it will execute the code in order to get the value of this property.
89

90
00:08:05,670 --> 00:08:13,500
So this is, in fact, a short version of the getter, and if you want to write it out completely, then you would
90

91
00:08:13,500 --> 00:08:14,850
write it like this.
91

92
00:08:14,850 --> 00:08:24,900
So inside a set of curly braces that has the get keyword in front of it, we specify the code that will
92

93
00:08:24,900 --> 00:08:30,260
run when another piece of code is trying to get the value of numberOfSlices.
