0 1 00:00:00,850 --> 00:00:07,870 Now, in previous lessons, we've been looking at the getter and setters for our computed properties, and you 1 2 00:00:07,870 --> 00:00:17,030 can see that the setter code is called the exact moment that the property is set with a new value. 2 3 00:00:17,320 --> 00:00:24,520 Now, if you didn't need to compute the value of a property with a getter, but you just wanted to monitor 3 4 00:00:24,670 --> 00:00:31,330 when the value of a property gets changed, then instead of using computed properties, Swift actually has 4 5 00:00:31,330 --> 00:00:38,380 something else which is called an observed property where you can trigger code when a property's value 5 6 00:00:38,440 --> 00:00:40,350 is changed. 6 7 00:00:40,360 --> 00:00:47,830 So, for example, if you wanted to detect when somebody tries to set the pizza size and inches to some 7 8 00:00:48,010 --> 00:00:55,140 unrealistic number, say, you try to buy a 33-inch pizzas, well, that doesn't really make any sense 8 9 00:00:55,150 --> 00:01:01,360 because I don't know anybody who sells 33-inch pizzas. If you know somebody, let me know in the comments, 9 10 00:01:01,360 --> 00:01:03,260 so I can go and eat those pizzas. 10 11 00:01:03,520 --> 00:01:09,640 But in our code, we don't really want this to ever happen because the calculations are performed are 11 12 00:01:09,640 --> 00:01:13,650 invalid because this is not a valid size. 12 13 00:01:13,660 --> 00:01:18,960 So in this case, we can actually turn this into an observed property. 13 14 00:01:18,970 --> 00:01:25,720 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 15 00:01:25,720 --> 00:01:27,190 curly braces. 15 16 00:01:27,190 --> 00:01:31,830 And inside here, we can set our property observers. 16 17 00:01:31,870 --> 00:01:40,800 So one of these observers is called willSet and another one is called didSet. 17 18 00:01:40,840 --> 00:01:44,270 Now, they essentially do exactly the same thing. 18 19 00:01:44,510 --> 00:01:52,310 And what they do is they monitor when this pizza in inches in property gets changed. Right before it gets changed, 19 20 00:01:52,310 --> 00:01:59,810 this block of code will trigger, willSet, and right after it changes, then this block of code will execute 20 21 00:02:00,200 --> 00:02:01,220 didSet. 21 22 00:02:01,220 --> 00:02:02,610 So let's try it out. 22 23 00:02:02,690 --> 00:02:07,880 First things first, you'll notice that we're getting a warning from Xcode because our pizzaInInches 23 24 00:02:08,120 --> 00:02:14,890 used to be a constant, and both computed properties and observed properties have to be variables. 24 25 00:02:14,930 --> 00:02:17,350 So we have to change that to a var. 25 26 00:02:17,600 --> 00:02:23,480 So, now inside willSet and didSet, there are two variables that we get access to and they're slightly 26 27 00:02:23,480 --> 00:02:24,030 different. 27 28 00:02:24,170 --> 00:02:28,970 So inside willSet, we have something called newValue. 28 29 00:02:29,420 --> 00:02:33,960 But inside didSet, we have something called oldValue. 29 30 00:02:34,050 --> 00:02:42,600 And so in this case, let's say, if I were to set the size pizzaInInches to, I don't know, 8. 30 31 00:02:42,830 --> 00:02:50,750 Now, at this point, as soon as I set it to a newValue, the willSet and didSet both get executed. With 31 32 00:02:50,750 --> 00:02:55,570 the willSet executing first, and then didSet coming straight afterwards. 32 33 00:02:55,700 --> 00:02:59,830 And inside willSet, we're printing the newValue. 33 34 00:02:59,900 --> 00:03:03,970 So that's the newValue that the property is being set to. 34 35 00:03:04,280 --> 00:03:10,580 And in the didSet, we can get access to this thing called oldValue which is the value that this property 35 36 00:03:10,580 --> 00:03:11,710 used to be. 36 37 00:03:11,720 --> 00:03:18,650 Now, if inside willSet, you wanted to access the oldValue, then you would simply use the current variables 37 38 00:03:18,650 --> 00:03:19,190 name. 38 39 00:03:19,220 --> 00:03:26,120 So you can say pizzaInInches. And inside didSet, if you wanted to get access to the newValue, again, 39 40 00:03:26,210 --> 00:03:33,560 you would access pizzaInInches. And that is because at the point when willSet is called, 40 41 00:03:33,560 --> 00:03:38,120 remember, this hasn't been changed yet, it's still equal to 10. 41 42 00:03:38,150 --> 00:03:42,880 So when you print its value, it will be equal to 10. 42 43 00:03:43,370 --> 00:03:49,060 But when you print the newValue that it's going to be set to, then that, of course, will be 8. 43 44 00:03:49,310 --> 00:03:50,460 And inside didSet, 44 45 00:03:50,500 --> 00:03:57,500 it's exactly the opposite situation because, at this point, the value of pizza inches has already been 45 46 00:03:57,500 --> 00:03:58,190 set, 46 47 00:03:58,250 --> 00:04:02,350 then the oldValue is equal to the previous value that it used to be, 47 48 00:04:02,540 --> 00:04:06,110 and pizzaInInches is now equal to 8. 48 49 00:04:06,110 --> 00:04:14,240 So what we can do here that can be quite useful is we can check to see if the newValue of 49 50 00:04:14,240 --> 00:04:18,280 pizzaInInches which, remember, can be accessed through the property name, 50 51 00:04:18,320 --> 00:04:25,810 so if pizzaInInches is greater than or equal to, let's say, 18, because our favorite pizza here, their 51 52 00:04:25,810 --> 00:04:28,040 largest pizza is 18 inches, 52 53 00:04:28,040 --> 00:04:33,600 so anything that's larger than 18 that gets inputted here is invalid. 53 54 00:04:33,800 --> 00:04:40,980 So if that happens, then we're going to print "Invalid size specified, 54 55 00:04:43,660 --> 00:04:54,160 pizzaInInches set to..." the largest size which is 18 inches. And we, of course, have to do that, so we can 55 56 00:04:54,160 --> 00:04:59,640 now tap into pizzaInInches, and we can set it to equal 18. 56 57 00:04:59,980 --> 00:05:08,140 So, now if I at a later point try to set this property to something completely unrealistic, say, a 33-inch 57 58 00:05:08,230 --> 00:05:15,720 pizza, then at this point we will get printed "Invalid size specified, pizzaInInches set to 18." 58 59 00:05:15,820 --> 00:05:22,680 And if you try to print pizzaInInches, even though it looks like it should print 33, right, 59 60 00:05:22,690 --> 00:05:23,980 because we set it here, 60 61 00:05:24,280 --> 00:05:27,000 actually, it's going to print 18. 61 62 00:05:27,100 --> 00:05:35,560 So that is an observed property and we use it to observe when its value is changed, and to perform 62 63 00:05:35,890 --> 00:05:37,980 various bits of code or logic 63 64 00:05:38,140 --> 00:05:44,830 when that happens. Now, in the next lesson, I've got a challenge for you to see if all of this makes sense. 64 65 00:05:44,830 --> 00:05:49,300 So head over there and you're going to build your own calculator using computed properties.