Preparing the data for our app

For our Food Delivery App, we want to add another screen that provides detailed information about the dishes that can be ordered. But before we can do that, we have to create a suitable data model to represent these products first.

Let’s create a new Swift file by clicking File – New – File in the Xcode Toolbar and then selecting Swift File. Let’s name this file Food.

Make sure you import the SwiftUI framework. For handling our food products, we create a struct called Food inside our Food.swift file.

import SwiftUI

struct Food {
    
}

Each product should contain the product’s title and the price of the product. Let’s create appropriate attributes for this purpose:

struct Food {
    let title: String
    let price: Double
}

Every Food instance should belong to one of four categories: burger, pasta, pizza or desserts. For being able to select and distinguish between those four categories, we have to create a corresponding enum.

Let’s create a new Swift file named Helper for storing this enum. We call the enum Categories and insert four different cases representing our four categories.

enum Categories {
    case burger
    case pasta
    case pizza
    case desserts
}

Back in our Food.swift file we can now add a new attribute category of type Categories to our Food struct. Each instance created out of this class will now be assigned to a certain category. 

struct Food {
    let title: String
    let price: Double
    let category: Categories
}

Because we will use our Food data model to wrap multiple Food instances into a List, we need it to conform to the Identifiable protocol. This is required to pass custom data model instances into Lists in SwiftUI (it’s also the reason why we imported the SwiftUI framework into our Food.swift file earlier). The Identifiable protocol has only one mandatory requirement: It requires the struct to contain an attribute to identify every instance by a unique id. Therefore, we’ll simply declare an id attribute of the type Int.

struct Food: Identifiable {
    let title: String
    let price: Double
    let category: Categories
    let id = UUID()
}

Providing the product data ➡️

Now that we have completed our Food data model, we can generate the products we will use for a List with detailed information about the foods the user can order.

We will use a separate file for this data. Therefore, create a new Swift file called FoodData.

Let’s use an array containing all the products we want for our app. Each product is an instance of the Food data model and – therefore – must be initialized with the corresponding product title, price, category and id.

Feel free to copy and paste the following array into your FoodData.swift file.

let foodData: [Food] = [
    Food(title: "Margherita", price: 5.99, category: .pizza),
    Food(title: "Prosciutto", price: 6.89, category: .pizza),
    Food(title: "Funghi", price: 6.99, category: .pizza),
    Food(title: "Calzone", price: 6.99, category: .pizza),
    Food(title: "BBQ Burger", price: 9.90, category: .burger),
    Food(title: "Cheeseburger", price: 7.90, category: .burger),
    Food(title: "Vegan Burger", price: 8.90, category: .burger),
    Food(title: "Pulled Pork Burger", price: 11.90, category: .burger),
    Food(title: "Spagetthi Bolognese", price: 8.90, category: .pasta),
    Food(title: "Penne all'arrabbiata", price: 7.90, category: .pasta),
    Food(title: "Aglio e olio", price: 7.90, category: .pasta),
    Food(title: "Cheesecake", price: 3.99, category: .desserts),
    Food(title: "Cupcake", price: 2.99, category: .desserts),
    Food(title: "Icecream", price: 2.99, category: .desserts)
]

Now we’re ready to build the interface for the detailed food screen of our Food Delivery App!

2 replies on “Preparing the data for our app”

“Let’s create a new Swift file by clicking File – New File in the Xcode toolbar and …”

You mean the “Xcode menubar”, right?

I’m writing to you from Italy. Thank you for the new version of your great book.
A single little piece of advice here: “Spagetthi Bolognese” is spelled “Spaghetti alla Bolognese” 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *