0
1
00:00:00,700 --> 00:00:04,970
Now, in the last lesson, we created what's called a getter,
1

2
00:00:05,050 --> 00:00:13,210
so a block of code that will execute whenever we try to get the value of a property. Now, computed properties
2

3
00:00:13,480 --> 00:00:21,900
also allow you to specify some code that should be executed when its value is set
3

4
00:00:22,060 --> 00:00:25,430
and this is called a setter.
4

5
00:00:25,450 --> 00:00:33,040
So, for example, in addition to this block for "get." underneath it. I can also write a block that's called
5

6
00:00:33,040 --> 00:00:43,240
"set," and the block of code inside the set will be executed whenever this property gets a new value set
6

7
00:00:43,300 --> 00:00:44,200
to it.
7

8
00:00:44,200 --> 00:00:52,270
So, for example, inside here, I can simply write a print statement that says "numberOfSlices
8

9
00:00:52,300 --> 00:00:56,980
now has a new value which is,"
9

10
00:00:56,980 --> 00:01:05,740
and there's a special variable that we can tap into which is called newValue, and this new value is
10

11
00:01:05,800 --> 00:01:12,570
equal to, essentially, exactly that, it's the newValue that's been given to numberOfSlices.
11

12
00:01:12,760 --> 00:01:21,910
So, for example, if we delete all of this and we simply write numberOfSlices is equal to 12, then you
12

13
00:01:21,910 --> 00:01:26,200
can see right away down here, we get printed numberOfSlices
13

14
00:01:26,200 --> 00:01:33,800
now has a new value which is 12, and that new value comes from this line here.
14

15
00:01:33,820 --> 00:01:43,810
So having this block of code, which we call the setter, allows us to tap into the exact moment when our property
15

16
00:01:43,960 --> 00:01:52,150
is set with a new value, and it allows us to use that new value in computations or various bits of code,
16

17
00:01:52,600 --> 00:01:59,180
and to execute it at the exact time when this property's value gets updated.
17

18
00:01:59,180 --> 00:02:06,190
Now, if you don't have a setter specified for your computed property, say, if we delete that block, you can
18

19
00:02:06,190 --> 00:02:08,080
see that you get an error here
19

20
00:02:08,320 --> 00:02:16,150
when I try to set a numberOfSlices equal to 12, and it says that "Cannot assign to value: 'numberOfSlices' is
20

21
00:02:16,240 --> 00:02:18,850
a get-only property."
21

22
00:02:18,850 --> 00:02:29,010
So because I only have a block specified for "get,"
then this computed property is now effectively read-only,
22

23
00:02:29,230 --> 00:02:35,660
so I cannot set it to have a new value, but I can use it whenever I need to.
23

24
00:02:35,680 --> 00:02:38,270
So this still works.
