Hey everybody what's going on this is Caleb with Debb slopes dot com.
And in this video we're going to go ahead and fix our model.
If you remember a few videos ago I said our model class had a big problem and let's take a look at it
now.
So pull open your X code project and we just created an instance of our model.
Right.
We set all the properties here.
But if we go into the model class here we see that these are just simple variables.
And because of that that means that they are publicly accessible and publicly modifiable.
So check it out.
Watch what I can do.
I can say Apple product name and I could change it to be Samsung s 8.
Can you see that.
Now if I build it we're not going to get any problems because that property is totally modifiable.
Now I'm going to go ahead and build this and show you why this is dangerous.
Think about this.
If this model class is loading data from a web site maybe you're downloading data from a Web site.
If it can be modified or customized by anything other than the class itself it could be dangerous.
So as you as you saw I modified I modified the name to say Samsung s 8 and check it out.
I really want the Samsung s 8 in space grey and it cost 999 I should not be able to modify the data
from my class like that.
That's very very dangerous.
So what we're going to do is we're going to change our class here to have what are called private variables
and a private variable is one that is only accessible within the class that it's involved in.
So we're going to make these private variables and this will basically lock it down so only our initialiser
can set the values.
OK.
Just like so.
But watch what happens if I go back to my controller and I try to build this and check it out.
It says name is X is inaccessible due to private protection level.
That's good that means we can't access it to change it anymore.
But it also says it's just plain inaccessible even if we're just trying to get the value.
So what we need to do is we need to find a way to access the value but only set it inside this class.
And there's a really elegant way to do this in swift.
We can make the variable public and it's public by calling public first.
So it's a public variable with a private sector so we can access it publicly meaning we can access it
from any other class but we can only set values inside this class.
That's pretty cool.
So go ahead and type public again.
And now what we need to do.
You can see it saying hey duplicate modifier you can't do that.
What we need to do is say that we want the setter to be private but the getter to be public so to do
that all we need to do is put parentheses type set and close parentheses right at the end.
So a public variable with a private setter very elegant very cool one of the awesome things we can do
in swift So go ahead and type set again.
And now if we build you'll notice we're still getting an error.
Why is that.
Can not assign to property names center isn't accessible.
Private sector public Geter so we can get the value but we can not set it.
Therefore our model is now totally safe.
And if I build and run my program again you'll see that it will build.
It'll run successfully and we should see the proper data that we want.
I really want the iPhone 10 in space grey and it costs nine ninety nine ninety nine.
Amazing super super cool.
Our products class here is now totally secure.
We can access its values publicly but we can only set them within the class when we initialize it.
Very cool this is a great way to make sure that our code is nice and safe.
So fantastic work.
Congratulations on finishing this course.
I really hope that you've learned a lot.
And yeah we'll see in the next target topic.
See you guys.