iOS App Development with Swift

Creating Data Model

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
iOS App Development with Swift

Check out a free preview of the full iOS App Development with Swift course

The "Creating Data Model" Lesson is part of the full, iOS App Development with Swift course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano walks through creating and connecting the category and product data models to allow importing dependencies. MenuManager and CartManager classes are also created to help download and store the application content.

Preview
Close

Transcript from the "Creating Data Model" Lesson

[00:00:00]
>> What's next in terms of what we can do here to improve the code, we need to create the data model, okay? So, a data model. Actually the data model, it's not a concept in Swift, it's just a concept in any app that you create. So in this case, we can use extracts and classes or both at the same time.

[00:00:23]
So, we are going to create a new file, but within a group. So I'm going to create first a new group, and I'm going to call this The Model. And within the model, I need at least two files, one for categories because actually the products are in categories coffees, teas, frappuccinos.

[00:00:44]
So I knew that category. Swift file is not a UI view so actually, we need to be careful not to pick a UI view. It's just data class so just data or data structure. So a category, okay? Like that and product or item, let's call it Product like so.

[00:01:10]
So inside here, we just need to create a structure, do you need classes, do we have OOP? I don't think so. So it can be just playing structures, like a product that contains a name, a description, a price, the image URL because we're going to download this from the network, okay?

[00:01:34]
So that's pretty simple, right? We know how to do that. Probably an ID as well. So, an ID, a name, a description, A price, you know how to do that at this point, I guess, and an image URL, maybe, we'll see, or an image as a string, something like that.

[00:02:00]
By the way you have the code, the companion website as well in case you wanna copy and paste. Let's see here we have the product, and the category, we're going to write in a minute. Also, maybe it's time to talk about this for a minute. So the image that is coming from the server, we haven't seen the Jason, the API yet, but it's a relative image, it's not a full qualified URL, okay?

[00:02:29]
So, something that you can do in a structure and classes is to create compute properties. A compute property is a property where we set the data immediately after like this. So, I'm saying that I will have an image URL in Swift, we do have a class called URL that represents a URL and you can construct the class from a string.

[00:02:56]
So, I can construct the URL with this URL, okay? In this URL the real image will be available, okay? Does it make sense?. And categories, we'll probably have is in structure category, will have a name, like bar name, and an array of products. How do you set a array?

[00:03:26]
It's, Brackets and then the type inside. So, an array of products. If you say it an array with bar, it's immutable array, so I can add elements, remove elements, if you send an array with length, it's an immutable element. Okay? Like that. I think I'm happy with that, that's my model, okay?

[00:03:56]
So, but it's not just the model because yeah, we have the model. So we have I think it's pretty similar to what I have here. Now that we have the model, we can update our product item a little bit. Product item is a view, remember that view, the view that we have there, that is rendering one item, just one item.

[00:04:19]
Well now that we do have like let's say the container for the data, we can create a dependency because to render an item, I have a dependency on the product itself, right? Make sense? So, I need to create a variable that can be called product of type product, oops, that's the type.

[00:04:48]
And then instead of Dami mash or instead of product name, I just need to say product name. Instead of this, I can keep the dollar sign, then use this kind of interpolation and say product price. And what about the image? I wanna download the image, right? From a server.

[00:05:19]
Fortunately for us, Swift UI has another version, another view called Async image. That instead of receiving an image name from my bundle, from my package, it receives a URL. So, in case you missed that, I have just added Async prefix, so Async image is just a different view.

[00:05:43]
And instead of Dami image, now I need to take the image URL, that is the compute property that I said before, okay? Makes sense? The only difference is that it says it's missing on, oops, the argument. I don't know why it's, okay, now it's working. You need to add URL column because that's how they set the initializer.

[00:06:11]
So now it's going to download that URL from the network. The problem now is that we have an error in the preview, first time we have an error in the preview, because the previews are actually compiled. It says, hey, I cannot render a product item without an actual product, makes sense?

[00:06:33]
Because now we have a dependency on a product. So, what I need to do here is to create a dummy product object with all the properties, okay? So it can be render, dummy, rather four, short for the preview, okay? Makes sense? An image bag, and put an empty image.

[00:07:00]
I can use a real image but we don't see them now. Okay, so, it gave me an error here building the scheme you can see it's saying, fail to build blah blah blah. If you click diagnostic we can actually see what's going on. It's really small, so it says, missing arguments for product in call.

[00:07:28]
I think this is the old version, Of this, or it gave me an error because actually the project is not compiling now. So, sometimes, pay attention here because this is important. Sometimes you receive errors in the preview, but the error is not actually even from your current file.

[00:07:49]
Maybe now the pressure is not compiling anymore, because you have an error somewhere else. And the preview says, I cannot preview these because the app is not running. In this case if you look here at the top there, it says that my brochure has one error and the error is that in the menu page I'm not passing a product that is mandatory, okay?

[00:08:17]
So for now, I can also create the dummy one because we are not downloading yet real products, okay? Does it make sense? So now, This should work back and the product item I mean the previous work, okay? With a dummy product, but now we kind of connected this, okay?

[00:08:50]
Make sense? Any question? We have a data model with new something else now. We need something or someone that will download the data or will actually do something with the data. What we need is, here we have a lot of architectural patterns, actually. So it depends on how are you building your app.

[00:09:18]
There are many architecture pattern like MVC, MVVM, blah, blah blah. We are trying to do this as simple as possible. So, what I'm going to create here, it's two other files that will act as managers, managers for all the menu and manager for the cart, okay? So I'm going to build managers, what is a manager?

[00:09:42]
Just a class, and we [INAUDIBLE] just classes here just to show you that we can actually use OOP if we want. So I'm going to create a menu manager. Again, manager is just a name that I'm setting, not an especial thing in Swift, it's a menu manager. So I'm going to create here a class called Menu Manager.

[00:10:05]
They idea is that it will contain here the categories, the array of categories, for example, okay? That's kind of the idea. And then I also need, The Cart Manager that will contain the order. I can call it Cart Order Manager, up to you. Remember, we can add cappuccino or some frappuccinos to the order, that's kind of the idea.

[00:10:32]
Okay? So, These items will actually be responsible for example for downloading the content and storing the content. But the next question is how my Views will access these managers, okay?does it make sense? So, let's say that here we have an array of categories. So I call this the menu, we can rename that later.

[00:10:59]
That is actually in array of category. And we can create an array here. So I have an array category with a name, Let's say Hot Coffee, well, one category and it brings an array of products so I can create an array, okay? Like so with a product. So I can actually do that.

[00:11:21]
Okay, I'm in this. I know we are going to download this from a server, but it just works, I can actually do this. So now I have a menu that has the category with a couple of products.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now