Everybody what's going on says Caleb with deps Lopes dot com.
And in this video.
Get excited.
We're going to create our model layer.
All right.
This app that we're going to build is basically going to just display some information about a particular
apple product that we want to buy.
So in order to actually set this up we're going to go ahead and create a class that will act as our
model layer to create instances of our Apple product so pull open your project we're going to right
click on the model folder in X code.
We're going to click new file like so.
And we don't need to do anything other than a basic swift file for a model class case so go ahead and
we're going to name this Apple product and go ahead and click Create your class we'll pull up here.
And the cool thing is we shouldn't actually need to use anything other than Foundation.
The most core fundamental swithe types and that is true or I guess we could also use custom types but
that doesn't require foundation.
So anyway let's move on and create our class.
Now you should know about classes you should have a good fundamental understanding of the swift programming
language before diving into this course.
So let's create a class called Apple product.
OK so remember this is our model layer so we need to think what is the blueprint what are we going to
help them build we can think of our model class as an instruction set to build an Apple product so we
can create both properties and functions K properties being variables or constants are things about
our class that make it that way.
So an Apple product for instance has a name iPhone 10 an Apple product has a color space gray or silver
an Apple product has a price 399 499 999 $15 billion.
It has a price but there are other things that make Apple products Apple products right.
They can do stuff they can make calls they can send messages they can connect to the Internet.
They can do all kinds of amazing things.
So that's where functions would come in in a class.
But for now we're just going to go ahead and create some properties that make an Apple product an Apple
product.
And so we're going to go ahead and begin by creating the variable var name and a name is a text data.
So we should use the type String var name is going to be of type string.
OK.
And it's going to tell me class Apple product has no initializers.
That's because we have not yet created an initialiser function but we will next.
So OK our Apple product has a name.
Next we need.
How about a color var color.
And you know what we could use all kinds of different things.
But let's just go ahead and use a string and pass in the word for whatever color our Apple device will
be next.
How about a price.
Var price.
And we're going to go ahead and use the number type double which includes a floating point decimal value
because you know the price is going to come in with a certain number of dollars and cents.
So there's that.
We have a name we have color we have price.
And let's just leave it at that for now.
So now it says we have no initializers.
We need to create a function that is going to essentially run through and set all the values for this
particular model layer and create an instance of it in memory that we can use.
So to do that go ahead and just call init and pass in three parameters.
We have one name of type String.
We have a second one color also of type string.
And apparently I can't type.
And we have.
Price of type double.
Now in order to set these properties all we need to do is to type self-caused name referring to this
property here.
We're going to set it to be equal to the name property passed in from the function and to do that we
just need to call name.
Then below that we're going to call self-caused color and set it to be color the 10:01 from the function
beneath that type self price.
And we're going to pass in price just like that.
So we have now successfully created the class but there's a problem a very big problem and I'm going
to show you what that is in a future video.
This is not a very safe class.
We're going to show you a better way to make it safer in a future video.
But for now this will work.
We have a class called Apple product.
It has three properties.
Name color and price and it also can be initialized.
We can create an instance of it and then use it in our application.
So cool so to show you how this works I'm actually going to create a constant called Apple product and
I'm going to set it to be equal to Apple product and watch this if I go ahead and put an open parentheses.
It's going to ask me to autocomplete and if I do you'll see I can pass in three properties.
This is our initialiser function being called.
So for my name I might say iPhone 10 for the color I might say space gray.
And for the price I might say nine hundred ninety nine point ninety nine for the retail price of the
iPhone 10.
Now the cool thing what I can do here is I can access these properties.
So now that I've created an instance of Apple product here and this constant I could print out Apple
product Dot and I could choose any of those properties Apple product name and I could print that to
the console.
I could print out Apple product color.
I could print out a Apple product up price if I were to cast it as a string but it's not because it's
double.
But anyway this is it guys we can now create an instance of our class.
But like I said there are some issues here.
These variables have an issue and we're going to fix it in a future video.
But guys amazing work.
We now have our model layer Apple product and we can use that to create an instance of our data model.
Very very cool stuff guys.
In the next video we're going to dive into the world of view and we're going to create a custom view
that we can actually display on the screen and I'll show you how it works I'll show you how we can interface
with the View Controller and help the View Controller to present that view.
So let's head over there now and I'll see you there.