
Tech • AI • Robotics • Game
A beginner-focused Swift and SwiftUI build shows how to create a working habit tracker in Xcode with add, delete, completion counting, persistent storage through SwiftData, and installation on a real iPhone using a free Apple ID.
Building an iPhone app with Xcode requires macOS, since Apple does not provide a Windows or browser version. A free Apple ID is enough to use the iOS simulator and install an app on a personal iPhone, while the $99-a-year Apple Developer Program is mainly needed for App Store distribution and removing free-account limits.
The app begins as a new iOS project named Habit App with SwiftUI selected for the interface and Swift for the language. The workflow focuses on a small set of Xcode tools: the file navigator, the central code editor, the preview canvas, and the issue navigator where build errors are collected.
The build introduces only the language features needed for the app: var for changeable values, let for constants, and core types such as String, Int, Double, and Bool. It also covers optionals and safe unwrapping with if let, along with functions, arrays, for-in loops, if-else, switch, and structs.
In SwiftUI, every screen element is a view, with common building blocks including Text, Image, and Button. Layout relies on VStack and HStack, while modifiers control styling such as size, weight, and padding. A simple counter demonstrates @State, showing how changing a stored value automatically refreshes the visible interface.
Common beginner mistakes are treated as expected parts of development rather than failures. Typical problems include missing brackets or commas, misspelled property names, and unsafe force-unwrapped optionals that can crash at runtime. The main discipline is to read the first reported Xcode error carefully, because one syntax issue often triggers several secondary warnings.
The app is intentionally limited to three core actions: display a list of habits, add new habits, and increment a completion count when a habit is marked done. The design uses two screens: a main list and an Add Habit sheet. Extra features such as streaks, dates, and categories are left out to keep the data model and code path simple.
Most SwiftUI views use structs, but the saved habit model uses a class because SwiftData needs shared reference semantics. Each Habit object holds only two properties: a name and timesCompleted, which starts at zero. Marking the class with @Model and attaching a modelContainer to the app entry point gives the app persistent storage with minimal setup.
The main view uses NavigationStack, a model context, and @Query to automatically fetch and track saved habits. If there are no records, the interface shows an empty-state prompt telling the user to tap the plus button. When habits exist, each row displays the habit name, its completion total, and a checkmark button that increments the count and is automatically saved by SwiftData.
The add screen is presented as a sheet and contains a form with a single text field. Cancel dismisses without changes, while Save inserts a new habit into the model context and closes the sheet. The save button remains disabled if the field is blank or contains only spaces, and deletion is handled from the main list with swipe-to-delete through onDelete.
Final refinements include setting an app icon in Assets.xcassets, assigning the display name Habit App, and checking appearance in both light and dark mode. Installation on a real iPhone is done by connecting the device to the Mac, trusting the computer, selecting the phone in Xcode, and pressing Run. Apps installed with a free Apple account expire after 7 days, after which they must be rebuilt and reinstalled.
The project demonstrates that a practical SwiftUI app can be built without mastering all of Swift in advance. By limiting scope and using SwiftData for persistence, the result is a functional iPhone habit tracker and a clear entry point into Apple platform development.
Ask a question