← All daysDay 5 of 49 · Part 2: Getting started with SwiftUI
Day 5

Xcode previews

These are the lesson notes for this day. Its build-along tutorial and flash cards have not been written yet.

Lesson 6

Xcode previews

A preview draws a view live in Xcode's canvas, beside its code, and redraws as you type. You declare one with the #Preview macro. This replaces most of the build–run–navigate cycle you know from UIKit: you can look at a single view in isolation, in several states at once, in dark mode, with very large text, or on a different device, without running the app.

The habit to build is one preview for every state a view can be in — empty, loading, loaded, error. That only works when a view can be created from sample data, which pushes you to pass data into views rather than fetch it inside them. That same habit is what makes views testable later. Xcode 26 added #Playground, which runs a snippet of ordinary code live in the same way. Xcode 27 adds coding agents, one of them specialized in SwiftUI.

Exercise

Create a new SwiftUI project. Build a small profile card and write three previews for it: the default, dark mode, and the largest Dynamic Type size. Do not launch the simulator at any point.

After this lesson you should be able to answer
  1. How do you declare a preview, and how do you preview one view in several configurations?
  2. Why do previews push you toward passing data into a view instead of loading it inside the view?
  3. What is #Playground for?
Resources