Swiftmas – Day 04 – Festive Food

Posted by

·

Who doesn’t love the food at this time of year. There is so much to choose from as well as many sweet treats. What will you create today?

For the Fourth prompt, we have Festive Food. What will you create?

I’m looking forward to seeing what you come up with today.

The holidays are here, and it’s the perfect time to try out some festive recipes! What if you could build an app to search for recipes by ingredients and dietary preferences? This tutorial will guide you step-by-step to create a Holiday Recipe Finder App using SwiftUI. I have left out the ability to add recipes…I’d like to see you try and add that yourself!

App Overview

This app will allow users to:

  • Search recipes by ingredients.
  • Filter recipes based on dietary preferences (e.g., Vegan, Vegetarian, Gluten-Free).
  • View recipe details, including the name, ingredients, and dietary info.

Let’s get started!

Getting Started


1. Set Up Your Project

You can find the sample project in the GitHub repository here

Create a new SwiftUI project in Xcode:

  1. Open Xcode and select Create a new project.
  2. Choose the App template, click Next.
  3. Enter your project name (e.g., “HolidayRecipeFinder”), set the interface to SwiftUI, and click Create.

Step 1: Create the Data Model

We’ll first define a model to represent recipes. Add the following Recipe struct to your project:

Explanation:

  • The Recipe struct includes:
    • id: A unique identifier.
    • name: The recipe name.
    • ingredients: A list of ingredients.
    • dietaryInfo: A list of dietary attributes (e.g., Vegan, Gluten-Free).
  • The Identifiable protocol allows SwiftUI to differentiate recipes in a list.

Step 2: Prepare Sample Recipes

To start, we’ll create a few sample recipes. Add this to the @State property in your ContentView:

These recipes will populate the app initially. Feel free to add as many as you like.


Step 3: Add Search and Filter Logic

Search Query

We’ll allow users to search for recipes by ingredient. Add this state property:

Dietary Filters

Users can also filter recipes by dietary preferences. Add this property:

Define the dietary options as a constant:

Filtering Recipes

Create a computed property to filter recipes dynamically:

Explanation:

  1. matchesQuery checks if the search query is empty or matches any ingredient.
  2. matchesDietary checks if no filters are selected or if the recipe matches the selected filters.

Step 4: Build the UI

Main Layout

Wrap everything in a NavigationView to display a title:

The background image adds a festive touch. Replace "bg" with the name of your background asset. Here is the one I used that is contained in the sample project.


Search Bar

Add a TextField for ingredient search:

This binds the user’s input to searchQuery.


Dietary Filters

Create filter buttons for dietary options:

Explanation:

  1. Each button toggles its respective filter in selectedDietaryFilters.
  2. The button’s background color changes based on whether the filter is active.

Recipe List

Display the filtered recipes in a List:


Step 5: Run Your App

Run the app on an iOS Simulator or device. Test the following features:

  • Search recipes by ingredient.
  • Apply and remove dietary filters.
  • View the dynamic list of recipes.

Complete Code

Here’s the complete code for your Holiday Recipe Finder:


What’s Next?

You can expand this app with features like:

  • Adding Recipes:  Allow the user to add to the list.
  • Recipe Cards: When the user selects the recipe, it opens the recipe card with full instructions.
  • Sharing: Allow users to share their recipe via email or text.

🎉 Congratulations! You’ve built a functional Holiday Recipe Finder in SwiftUI. Now you’re ready to search for recipes and make delicious festive dishes! Happy coding!


Discover more from

Subscribe to get the latest posts sent to your email.

thecodingsprite avatar

About the author

Hi! My name is Billie, my friends call me Billie Boo. I am a self taught iOS developer with a background in computer science, animation, graphic design & web design. I love sharing my knowledge & projects with the world & that is my mission for this blog. It’s never too late or too hard to follow your dreams.