Back to Swift Reference
🛠️ Xcode & AI WorkflowBeginnerXcode 15+

#Preview & Xcode Previews

Iterate on SwiftUI views instantly with multiple previews, sample data and traits.

#Preview replaces PreviewProvider. Declare as many previews as you like, each with a name and traits such as landscape or size-that-fits.

For SwiftData views, provide an in-memory container with sample data so previews never touch real storage.

preview-macro.swift
1#Preview("Empty state") {
2    HabitList()
3        .modelContainer(for: Habit.self, inMemory: true)
4}
5
6#Preview("With data", traits: .sizeThatFitsLayout) {
7    let container = try! ModelContainer(
8        for: Habit.self,
9        configurations: ModelConfiguration(isStoredInMemoryOnly: true)
10    )
11    container.mainContext.insert(Habit(name: "Meditate"))
12    container.mainContext.insert(Habit(name: "Run 5K"))
13    return HabitList().modelContainer(container)
14}
15
16#Preview {
17    @Previewable @State var isOn = true
18    Toggle("Notifications", isOn: $isOn).padding()
19}

Common pitfalls

  • If previews crash, check the diagnostics button — a missing environment object or container is the usual cause.

Still unclear? Ask BeeAI about #Preview & Xcode Previews.

Get a tailored explanation, or paste your own code for a Swift 6 review. Free members get daily questions — Pro is unlimited.

Ask BeeAI

Become a Founding Pro member: unlimited BeeAI, upcoming Swift paths and every new reference entry first.

See Pro Plans