Plug them back ladies and gentlemen Mark Price here at DEF slopes dot com and we're going to continue
building our coater swag app.
So far we have the collection view cell in place from the last video the UI for it at least.
And I think what we need to do now is start building out some of our data Carco the data in the app
as well as our data model that we're going to need to host that data.
And the reason why I like to build you whine and go into the data before I actually maybe work in the
view controllers is because sometimes you can't move forward without data or you can build your view
controller.
But how do I pass data from one screen to the other if it doesn't even exist.
And how can I show data on the cell even if it doesn't exist.
And so close to that first so what we're going to do is we're going to go into our model folder here.
We're going to create a new file We're going to call this a swift file here and we're going to call
this product.
You can have multiple products just like you can have multiple categories.
We'll make this a struct as well.
Always use a struct unless you absolutely need a class.
That's the rule of thumb so let's make a struct.
All right and what we're going to do is just just like we do with the category we're going to have the
data that we need chrism variables initialiser things like that so private set public var and we're
going to say tidal type String private set public var a price y making price as a string instead of
a dollar sign while we're not actually using those numbers X for anything except the display.
And when you make a purchase it would probably happen on the back end anyway.
And so a string is completely appropriate and it will become a string at one point anyway.
When you have to display.
And then the textfield so public var image name like we've done before as well to of type String.
Let's create our initialiser at just press enter on the autocomplete there and we're going to say title
of type String price of type String image name of type string.
And then just like we did before on the category's self that title equals title itself that price equals
price and self dot image name equals image name.
All right we've got our product model.
It's looking good.
Let's just put this space here for good measure and let's go to our services now and what we want to
do is we want to create a service that allows us to get whatever product that we need.
And we know that we have hats hoodies et cetera et cetera et cetera.
So what we're going to do is create a few different variables here for those products.
So what we're going to do and we'll start off with hats just for fun.
Private let hat equals and we're just like we did appear with categories.
We're going to create some products so it's going to be a product and then it's going to ask for certain
things like title price et cetera.
We've done this before right.
So I'm getting this information from our merchandise images.
So we've got these hats and hoodies and things like that.
And I'm just going to make the prices up and everything.
So just follow along.
And let's make some data.
So the title is going to be Deb slopes logo graphic beanie.
OK.
The price is going to be $18 and the image name is hat 01 PMG.
OK.
So there's our first product.
We're going to type all these out.
So this is really boring for you can speed up a copy and paste the code in the project.
The reason why I don't copy and paste all the stuff over again is because it's important for you to
type as a new programmer and never ever has anybody ever been like I wish were sold to copy and paste
more when they're teaching.
It's always the opposite.
Why are they keeping a pacing I can't keep up I don't know what they just did.
So we're just going to write these all out here.
No big deal.
It's all part of the process.
Lots of fun.
Logo hat black.
I'm putting in different products here and these values actually don't matter.
The only one that really matters here is the image name because we're pulling it from the desk.
This other stuff is just titles and descriptions so you can put whatever you want there actually make
another product.
I am putting commas at the end of these here so we're going to have 0 3 now so this is going to be dev
slopes logo hat white.
The price is $22 and the image name is hat 0 3 that P and G comma and the final product in the hat series
is going to be the slopes logo snap back.
And this price will make it a little bit cheaper.
$20.
And the image name is going to be zero for you as crazy as when I was a kid like flex felt hats were
all the rage like all the rage and now you can hardly find them anywhere.
Everyone's into the snacks which were all the rage in the 80s.
You know less like truck drivers where you know there are beer bellies and their hair coming out of
the front of their hats it was disgusting.
And now it's like it's back in style again.
You go to any store like Zumiez or Pac Sun or anything like that.
There's no flex it's all nasty snap backs.
Well anyway hats.
So next thing we're going to do is the hoodies.
All right so private let hoodies equals and just make more products.
And again these would normally be on a server unless your app is hardcoded like this which a lot of
new developers or simple apps have hardcoded information like this.
So this is their slopes logo the grey The price is going to be $32.
And the image name is going to be Pootie 0 1 p and G comma.
You know we can do here is we can just copy and paste it from our existing stuff.
So 1 how many Hoodie's we have forced to.
So what we can do is change the images so I know this is the two and three and four.
And then we can just change that text.
So the second one is Dove soaps logo be red.
Same price and then slopes low def slopes the gray OK same price in the last one the slopes the black
like so little bit or doing it that way.
So there's our hoodies.
Let's do our shirts and then our shirts we have five of them.
So private let shirts equals.
All right.
And still a product like so.
And the title for a shirt is going to be Dev's slopes logo shirt black and the price is $18.
So Image Name is going to be shirt 0 1 that P and G and will do the same thing here will copy and paste
these few items from what we've already typed and then we'll just modify them two three four five.
Get rid of that last comma there.
Otherwise we'll have a syntax error.
And so the first one is def slopes logo shirt black slopes badge.
So this one's depth slopes badge shirt light gray and shirt 0 2 and the third one is def slopes logo
shirt red OK.
And then the third one is hustle delegate gray or the excuse me the fourth one is Cosel delegate grab
by the way these shirts are all available at coater swag dot com available to purchase.
So you can look cool just like me even though I'm not wearing any of this right now.
Hustle delegate.
I do own the merchandise though.
So 0 1 0 2 0 3 on the shirt 0 4 on this shirt.
Let's just change the price.
And the second one $219 just for fun.
And on the fifth one this one is kickflip studios studios black.
This is our games to do that we are building up and shert 05 that looks good for the shirts.
The last one is digital good and other digital goods has nothing.
So what we want to do is say private let digital goods equals and we'll just make an empty array of
type product OK.
We can't just make an empty array like this because in swift You have to declare a type OK whether it's
explicitly the card or implicitly rather inferred.
You have to have a type.
So what we're going to is going to create an empty array of type product and putting the print that
is at the end actually creates an empty array.
OK.
And there's nothing in it.
Why did we have to do that at all well when our collection view was feeding off of this data.
It's going to need to get an array of some kind.
And if we don't have an empty array we'll have something that's nil and the app will crash.
OK.
So this is very important.
OK.
So very cool.
So we got our actual data now and what I want to do now is create a function that's like get products
for a specific category so basically the ideas you pass and a category title and it gets the appropriate
product array for that title.
So we're in a funk get products move this down a little here.
Move up rather to get products for category title and the variable named title we just give the parameter
a name and it's going to return an array of type product and what we'll do is we'll create a switch
statement a switch statement to help us decide which which array to return.
And so we're going to say title.
So we're going to we're going to see what title was passed in here and in this case if the case is shirts
we're going to return shirts but we don't have any functions yet for that.
So next thing we need to do is create a few empty functions so we can finish off or switch David so
function get hats and we're going to return a product like so and let's do the other ones.
Phunk get these return of type product on X coats.
Stick with me or stop going all the way to the bottom here phunk get shirts up type
product like so and then digital goods phunk get digital goods type product.
All right now we're not returning a thing yet so it's going to yell at us but at least we can finish
this now so now with the shirts I can say get shirts and then I'll do is copy this for the other three
categories here so one two three.
So get shirts or hats or booties doesn't matter what order it's in or digital.
And we can do default here.
The default will be get shirts in case it didn't match any of these cases here which it will of course.
But to get that and then what is is going to be get hoodies it's yelling at us again because we're not
returning anything.
And then this one's going to be good digital goods.
So does this make sense.
We obviously need to know.
We need to know which type of category that we're getting the product array for.
OK.
And what we need to do in each of these cases is return them return return return return and return
cool.
So that looks good to me.
And then in these cases here which need to return the appropriate data so we can return that's return
but he's return shirts.
And so what I want and weirdo.
All right shirts and return digital goods.
So all these heirs should go here.
OK.
So what's going to happen is like our view controller when it's filling out the information for the
collection to sell it's going to when we go onto the screen.
So from the product when we select that specific product let's say we select shirts we're going to select
shirts go to the next view controller with the collection view and then it's going to call this function
get products for category.
Time will pass in the title and then we'll return the appropriate array of data based on that title
and this is also similar how you might do it if you were talking to a web server that you would pass
up.
Hey I want the shirts and you would let the server know and the server would send you back this data.
Very very similar.
So we got our hats hoodies products etc. This is looking really good.
So cool.
I'm going to call this video done.
We just kind of set up our data and now we have everything that we need to actually go forth and build
out our collection to sell and show data in it and we're getting really close here.
So it's not that complex compared to the table view very very similar.
And we're making great progress so that's it for now.
Marc price advice Lopes dotcom.
See you soon.