1
00:00:06,970 --> 00:00:10,400
Hey everybody what's going on this is Caleb with Debb slopes dot com.

2
00:00:10,420 --> 00:00:14,620
And in this video we're going to go ahead and fix our model.

3
00:00:14,630 --> 00:00:20,800
If you remember a few videos ago I said our model class had a big problem and let's take a look at it

4
00:00:20,800 --> 00:00:21,070
now.

5
00:00:21,070 --> 00:00:27,600
So pull open your X code project and we just created an instance of our model.

6
00:00:27,670 --> 00:00:28,440
Right.

7
00:00:28,450 --> 00:00:29,730
We set all the properties here.

8
00:00:29,740 --> 00:00:36,470
But if we go into the model class here we see that these are just simple variables.

9
00:00:36,670 --> 00:00:43,570
And because of that that means that they are publicly accessible and publicly modifiable.

10
00:00:43,570 --> 00:00:44,410
So check it out.

11
00:00:44,440 --> 00:00:45,530
Watch what I can do.

12
00:00:45,790 --> 00:00:54,410
I can say Apple product name and I could change it to be Samsung s 8.

13
00:00:54,640 --> 00:00:55,510
Can you see that.

14
00:00:55,540 --> 00:01:02,500
Now if I build it we're not going to get any problems because that property is totally modifiable.

15
00:01:02,530 --> 00:01:06,210
Now I'm going to go ahead and build this and show you why this is dangerous.

16
00:01:06,220 --> 00:01:07,040
Think about this.

17
00:01:07,060 --> 00:01:13,560
If this model class is loading data from a web site maybe you're downloading data from a Web site.

18
00:01:13,660 --> 00:01:20,090
If it can be modified or customized by anything other than the class itself it could be dangerous.

19
00:01:20,110 --> 00:01:27,500
So as you as you saw I modified I modified the name to say Samsung s 8 and check it out.

20
00:01:27,520 --> 00:01:34,150
I really want the Samsung s 8 in space grey and it cost 999 I should not be able to modify the data

21
00:01:34,180 --> 00:01:35,850
from my class like that.

22
00:01:35,860 --> 00:01:37,630
That's very very dangerous.

23
00:01:37,630 --> 00:01:44,680
So what we're going to do is we're going to change our class here to have what are called private variables

24
00:01:44,980 --> 00:01:52,520
and a private variable is one that is only accessible within the class that it's involved in.

25
00:01:52,540 --> 00:01:59,290
So we're going to make these private variables and this will basically lock it down so only our initialiser

26
00:01:59,590 --> 00:02:01,130
can set the values.

27
00:02:01,130 --> 00:02:01,460
OK.

28
00:02:01,510 --> 00:02:02,560
Just like so.

29
00:02:02,920 --> 00:02:08,130
But watch what happens if I go back to my controller and I try to build this and check it out.

30
00:02:08,170 --> 00:02:13,180
It says name is X is inaccessible due to private protection level.

31
00:02:13,180 --> 00:02:16,660
That's good that means we can't access it to change it anymore.

32
00:02:16,780 --> 00:02:22,650
But it also says it's just plain inaccessible even if we're just trying to get the value.

33
00:02:22,660 --> 00:02:30,150
So what we need to do is we need to find a way to access the value but only set it inside this class.

34
00:02:30,160 --> 00:02:33,470
And there's a really elegant way to do this in swift.

35
00:02:33,580 --> 00:02:39,260
We can make the variable public and it's public by calling public first.

36
00:02:39,310 --> 00:02:47,680
So it's a public variable with a private sector so we can access it publicly meaning we can access it

37
00:02:47,680 --> 00:02:52,590
from any other class but we can only set values inside this class.

38
00:02:52,600 --> 00:02:53,560
That's pretty cool.

39
00:02:53,560 --> 00:02:55,390
So go ahead and type public again.

40
00:02:55,600 --> 00:02:56,690
And now what we need to do.

41
00:02:56,710 --> 00:02:59,970
You can see it saying hey duplicate modifier you can't do that.

42
00:03:00,040 --> 00:03:05,800
What we need to do is say that we want the setter to be private but the getter to be public so to do

43
00:03:05,800 --> 00:03:11,770
that all we need to do is put parentheses type set and close parentheses right at the end.

44
00:03:11,860 --> 00:03:17,980
So a public variable with a private setter very elegant very cool one of the awesome things we can do

45
00:03:18,220 --> 00:03:21,130
in swift So go ahead and type set again.

46
00:03:21,280 --> 00:03:25,910
And now if we build you'll notice we're still getting an error.

47
00:03:25,930 --> 00:03:27,120
Why is that.

48
00:03:27,210 --> 00:03:31,970
Can not assign to property names center isn't accessible.

49
00:03:32,050 --> 00:03:37,300
Private sector public Geter so we can get the value but we can not set it.

50
00:03:37,300 --> 00:03:40,180
Therefore our model is now totally safe.

51
00:03:40,180 --> 00:03:43,720
And if I build and run my program again you'll see that it will build.

52
00:03:43,850 --> 00:03:49,060
It'll run successfully and we should see the proper data that we want.

53
00:03:49,090 --> 00:03:54,250
I really want the iPhone 10 in space grey and it costs nine ninety nine ninety nine.

54
00:03:54,250 --> 00:03:56,860
Amazing super super cool.

55
00:03:56,860 --> 00:04:00,520
Our products class here is now totally secure.

56
00:04:00,580 --> 00:04:06,820
We can access its values publicly but we can only set them within the class when we initialize it.

57
00:04:06,820 --> 00:04:10,210
Very cool this is a great way to make sure that our code is nice and safe.

58
00:04:10,210 --> 00:04:11,740
So fantastic work.

59
00:04:11,740 --> 00:04:14,380
Congratulations on finishing this course.

60
00:04:14,410 --> 00:04:15,840
I really hope that you've learned a lot.

61
00:04:15,860 --> 00:04:18,100
And yeah we'll see in the next target topic.

62
00:04:18,100 --> 00:04:19,630
See you guys.
