Swiftmas – Day 03 – Gift Organizer

Posted by

·

I love organaziation, so why not organise the gifts too!

For the Third prompt, we have Gift Organizer. What will you create?

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

In today’s tutorial, we’ll walk through how to build a Gift Exchange Organizer app using SwiftUI. This app will allow users to add participants, assign gifts randomly, and display the assignments in a list. This is perfect for holiday gift exchanges or Secret Santa events.

By the end, you’ll have a fully functional app with:

  1. A sleek interface to add participants & gifts.
  2. A organised list with gift sharing allocations.
  3. Festive touches like color-coded rows and a background image.

Let’s dive in!

Getting Started

You can find the sample project in the GitHub repository here


Step 1: Setting Up the Project

Before we dive into the code, make sure you have Xcode installed. Create a new SwiftUI project and name it GiftGivingOrganizer.

Once your project is created, you can start adding the necessary UI components.


Step 2: Defining the State Variables

The app will have the following data:

  • Participants: A list of names of people who are involved in the gift exchange.
  • Gifts: A list of gifts that will be assigned to the participants.
  • Assignments: The final list of assignments showing which participant gets which gift.

To manage these, we’ll use the @State properties in SwiftUI, which will automatically update the UI whenever the data changes.

Here’s how we define them in our ContentView:


In this tutorial, we will build a SwiftUI app to help organize a gift exchange. The app lets users add participants, input a list of gifts, and assign gifts randomly to each participant. Follow along to create your own app!

What Each Variable Does:

  • participants: Stores the list of names entered by the user.
  • newParticipant: Holds the name currently being entered in the text field.
  • gifts: Stores the list of gifts entered by the user.
  • newGift: Holds the gift currently being entered in the text field.
  • assignments: Contains the final list of participant-gift pairs after assignment.

Step 3: Create the User Interface

Now let’s add the UI components. Update the body of the ContentView:

Explanation of the Code:

  1. The TextField allows users to input participant names.
  2. The Button appends the new participant to the participants array and clears the text field.
  3. The list of participants is displayed below the input field.

Step 4: Add a Gift Input Section

Next, we create a similar UI section for entering gifts. Add this after the participant section:

Explanation of the Code:

  1. Similar to the participant section, the TextField takes the gift input, and the Button adds it to the gifts array.
  2. The current list of gifts is displayed dynamically.

Step 5: Add the Gift Assignment Button

We need a button to assign gifts randomly. Add the following code after the gift input section:

Explanation of the Code:

  1. The button is disabled until both participants and gifts are added.
  2. When tapped, it calls the assignGifts function to randomly pair participants with gifts.

Step 6: Display the Assignments

Finally, add a List to display the results of the gift assignments. Place the following code below the button:

Explanation of the Code:

  1. List is used to show the participant and their assigned gift.
  2. Each row is styled with a yellow background for better visibility.

Step 7: Add the Gift Assignment Logic

We need a function to randomly assign gifts to participants. Add this method inside the ContentView struct:

Explanation of the Code:

  1. The gifts array is shuffled to ensure randomness.
  2. Each participant is assigned the first gift in the shuffled list, which is then removed.

Step 8: Add a Background

For a festive touch, include a blurred background image. Add this inside the NavigationView:

Replace "festive-bg" with the name of your background image asset. Here is the link to the one used on the sample project. It will also be included in the sample project in the GitHub repo.


Complete Code

Here’s the full code for reference:


Run the App

Build and run the app in the iOS Simulator. Test the following:

  • Add participants and gifts.
  • Assign gifts using the button.
  • View the randomized assignments.

Congratulations! You’ve successfully built a Gift Exchange Organizer in SwiftUI. 🎁

Conclusion

This simple Gift Exchange Organizer app allows users to input participants and gifts, then randomly assign the gifts to participants with a simple button click. It’s a fun and useful tool for organizing Secret Santa or other holiday gift exchanges.

Feel free to customize this app with your own styling and features, such as sending invitations or managing gift lists over multiple sessions!


Further Improvements

  • Persistence: You could add functionality to persist the participants and gift list using UserDefaults, SwiftData or CoreData, so users don’t lose their data when the app restarts.
  • Advanced Features: Add the ability to exclude assigning a gift to the same person (in case of a Secret Santa where each person gives and receives a gift).

Happy coding! See you tomorrow.🎄


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.