How to Build a Habit Tracker App with Xcode and AI

Team Alex · April 6, 2025

Tutorial

Estimated completion time: 3 min read

How to Build a Habit Tracker App with Xcode and AI

Overview

We're going to build a habit tracker app. This app will allow users to list their habits, track their progress, and maintain consistency on their journey towards self-improvement. Let's explore the exciting world of app development with Xcode and our AI friend, Alex, working together.

Our app will include features such as listing habits, marking habits as done, and calculating streaks. We'll use SwiftUI for our user interface, and we'll store data using UserDefaults for persistence.

Prerequisites:

Implementation Steps

1. Project Setup & Planning

Start by creating a new Xcode project. We'll use the SwiftUI App template. Let's call the project "Habit Tracker."

Next, we're going to plan our app's main features and data models. We will need a Habit model that includes a title, a description, and a counter for the habit streak.

Start by manually creating a simple model like this:

struct Habit: Identifiable, Codable {
    let id = UUID()
    var title: String
    var description: String
    var streak: Int
}

If you'd like help adjusting or generating this structure, you can describe what you want to Alex in plain language, like: "Create a Habit struct with a title and description as Strings and a streak as an Int."

2. Core Features

Let’s build our app’s UI using SwiftUI.

Start by creating a list view to display habits:

struct HabitListView: View {
    @State private var habits: [Habit] = []
 
    var body: some View {
        NavigationView {
            List(habits) { habit in
                VStack(alignment: .leading) {
                    Text(habit.title).font(.headline)
                    Text(habit.description).font(.subheadline)
                    Text("Streak: \(habit.streak)")
                }
            }
            .navigationTitle("My Habits")
        }
    }
}

If you want to quickly build or tweak views, you can ask Alex something like: "Create a SwiftUI List view that shows habit title, description, and streak."

Next, implement functionality to add and update habits manually, or ask Alex to help scaffold it: "Add a form to create a new Habit and append it to the list."

Be mindful of SwiftUI state. If something’s not updating, Alex can help you debug.

3. Data & Storage

We’re going to use UserDefaults to persist data. Start with basic save/load functions:

func saveHabits(_ habits: [Habit]) {
    if let encoded = try? JSONEncoder().encode(habits) {
        UserDefaults.standard.set(encoded, forKey: "SavedHabits")
    }
}
 
func loadHabits() -> [Habit] {
    if let data = UserDefaults.standard.data(forKey: "SavedHabits"),
       let decoded = try? JSONDecoder().decode([Habit].self, from: data) {
        return decoded
    }
    return []
}

You can also ask Alex for help with this, like: "Create a function to save an array of Habits to UserDefaults using Codable."

4. Polish & Refinement

Add animations to improve UX, like animating list insertions. You could wrap your add logic in withAnimation {}.

Handle error or loading states with alerts or placeholders. Want help? Ask Alex: "Show a spinner while habits are loading."

For final performance tweaks, Alex can also review your code and suggest improvements.

Next Steps

In the next part of this tutorial series, we’ll add more advanced features like habit reminders and in-app achievements.

Want a challenge? Try adding:

  • Tags or categories for habits
  • Daily progress charts
  • Cloud sync

Conclusion

Congrats on building your first app with Xcode and Alex! You’ve built a habit tracker app, learned about SwiftUI and UserDefaults, and seen how an AI assistant can simplify your workflow.

Remember, Alex doesn’t require perfect instructions—it understands general ideas and helps turn them into code. Try it out at alexcodes.app with a free trial. Happy coding!