How to build a Stopwatch app with Xcode and AI

Team Alex · April 6, 2025

Tutorial

Estimated completion time: 3 min read

How to build a Stopwatch app with Xcode and AI

Overview

In this tutorial, we will be creating a simple yet functional Stopwatch app using Xcode, Swift, and SwiftUI. Additionally, we'll use Alex, a powerful AI assistant that can streamline your iOS development process.

The final app will have features like start, stop, and lap times, all built using SwiftUI for a native iOS look and feel. Before we start, make sure you have the following tools installed:

  1. Xcode
  2. Alex

Implementation Steps

1. Project Setup & Planning

Begin by creating a new Xcode project: open Xcode, select "Create a new Xcode project," then select the "App" template under the iOS section. Name the project "Stopwatch."

Now, let's plan our features. Our app should:

  • Display the elapsed time (in seconds)
  • Have start, stop and lap buttons
  • List lap times

Let's use Alex to create our initial data model. Just speak or type your model structure using general language, and Alex will translate it into Swift code. For our Stopwatch, we may describe a Lap struct with lapNumber and lapTime properties.

2. Core Features

To build the UI, we'll use SwiftUI. We want a large timer display at the top, followed by the control buttons, and a list of lap times at the bottom.

Here's an example of how we might describe our interface to Alex: "A VStack with a Text for the timer, an HStack for start and stop buttons, and a List for the lap times." We can then edit and customize the generated code as needed.

Next, let's wire up the logic. We'll need to update our timer display every second and manipulate the timer controls according to the user's interaction. Again, Alex can help generate initial instances and functions for our Stopwatch.

3. Data & Storage

Our Stopwatch app will need to store lap times. For simplicity, we'll save this data locally using UserDefaults.

Describe to Alex a function to save and retrieve the array of Lap objects. Alex will generate Codable-friendly boilerplate code, which we can then adjust for our specific requirements.

We also need to manage the state of our app. For this, we'll use SwiftUI’s @State and @Binding property wrappers to manage the timer's running state and the lap times data array, respectively.

4. Polish & Refinement

Finally, let's refine our app. We can add animations for changing lap times and handle potential error states. We should also ensure we're not wasting resources by running the timer when it's not needed.

Alex can offer useful suggestions here, such as identifying redundant code or suggesting ways to improve our app's structure.

Next Steps

Congratulations on building your Stopwatch app!

To continue learning, consider adding features like the ability to reset the timer, color-coding lap times, or adding haptic feedback.

Conclusion

In this tutorial, we built a Stopwatch app from scratch using Xcode and SwiftUI, and we used Alex, an AI assistant, to streamline the process. This powerful AI tool can generate Swift code from general language descriptions, which can immensely speed up the development process, especially for beginners.

Try Alex for free at Alex Codes and experience how AI can simplify your iOS development process.

Happy coding!