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