Now, in previous lessons, we've been looking at the getter and setters for our computed properties, and you 1
can see that the setter code is called the exact moment that the property is set with a new value. 2
Now, if you didn't need to compute the value of a property with a getter, but you just wanted to monitor 3
when the value of a property gets changed, then instead of using computed properties, Swift actually has 4
something else which is called an observed property where you can trigger code when a property's value 5
is changed. 6
So, for example, if you wanted to detect when somebody tries to set the pizza size and inches to some 7
unrealistic number, say, you try to buy a 33-inch pizzas, well, that doesn't really make any sense 8
because I don't know anybody who sells 33-inch pizzas. If you know somebody, let me know in the comments, 9
so I can go and eat those pizzas. 10
But in our code, we don't really want this to ever happen because the calculations are performed are 11
invalid because this is not a valid size. 12
So in this case, we can actually turn this into an observed property. 13
So, for example, let's say that pizza in inches is set to 10 by default, and then we can open a set of 14
curly braces. 15
And inside here, we can set our property observers. 16
So one of these observers is called willSet and another one is called didSet. 17
Now, they essentially do exactly the same thing. 18
And what they do is they monitor when this pizza in inches in property gets changed. Right before it gets changed, 19
this block of code will trigger, willSet, and right after it changes, then this block of code will execute 20
didSet. 21
So let's try it out. 22
First things first, you'll notice that we're getting a warning from Xcode because our pizzaInInches 23
used to be a constant, and both computed properties and observed properties have to be variables. 24
So we have to change that to a var. 25
So, now inside willSet and didSet, there are two variables that we get access to and they're slightly 26
different. 27
So inside willSet, we have something called newValue. 28
But inside didSet, we have something called oldValue. 29
And so in this case, let's say, if I were to set the size pizzaInInches to, I don't know, 8. 30
Now, at this point, as soon as I set it to a newValue, the willSet and didSet both get executed. With 31
the willSet executing first, and then didSet coming straight afterwards. 32
And inside willSet, we're printing the newValue. 33
So that's the newValue that the property is being set to. 34
And in the didSet, we can get access to this thing called oldValue which is the value that this property 35
used to be. 36
Now, if inside willSet, you wanted to access the oldValue, then you would simply use the current variables 37
name. 38
So you can say pizzaInInches. And inside didSet, if you wanted to get access to the newValue, again, 39
you would access pizzaInInches. And that is because at the point when willSet is called, 40
remember, this hasn't been changed yet, it's still equal to 10. 41
So when you print its value, it will be equal to 10. 42
But when you print the newValue that it's going to be set to, then that, of course, will be 8. 43
And inside didSet, 44
it's exactly the opposite situation because, at this point, the value of pizza inches has already been 45
set, 46
then the oldValue is equal to the previous value that it used to be, 47
and pizzaInInches is now equal to 8. 48
So what we can do here that can be quite useful is we can check to see if the newValue of 49
pizzaInInches which, remember, can be accessed through the property name, 50
so if pizzaInInches is greater than or equal to, let's say, 18, because our favorite pizza here, their 51
largest pizza is 18 inches, 52
so anything that's larger than 18 that gets inputted here is invalid. 53
So if that happens, then we're going to print "Invalid size specified, 54
pizzaInInches set to..." the largest size which is 18 inches. And we, of course, have to do that, so we can 55
now tap into pizzaInInches, and we can set it to equal 18. 56
So, now if I at a later point try to set this property to something completely unrealistic, say, a 33-inch 57
pizza, then at this point we will get printed "Invalid size specified, pizzaInInches set to 18." 58
And if you try to print pizzaInInches, even though it looks like it should print 33, right, 59
because we set it here, 60
actually, it's going to print 18. 61
So that is an observed property and we use it to observe when its value is changed, and to perform 62
various bits of code or logic 63
when that happens. Now, in the next lesson, I've got a challenge for you to see if all of this makes sense. 64
So head over there and you're going to build your own calculator using computed properties.