TechnologiesSwiftUI

PreviewPlatform enum

iOSmacOStvOSwatchOSvisionOSiOS 13.0+✓ renders

Platforms that can run the preview.

How it works

PreviewPlatform is an enumeration that names the device family a preview should render for, letting you target a specific Apple platform when Xcode's canvas can't infer it from the active build destination. Reach for it when a view's appearance depends on the platform — for example to preview a watch-sized layout while building for iOS, or to confirm a design holds up on macOS and tvOS. Each case identifies one platform, so the type acts as a small, fixed vocabulary you pass into the preview system or branch on in your own code.

  1. Choose a platform case

    PreviewPlatform exposes one case per supported device family, and you select the target by referring to a case directly. The example stores a fixed choice in let platform: PreviewPlatform = .iOS, declaring that this view is meant to be seen as it appears on iOS.

  2. Use .iOS, .macOS, .tvOS, and .watchOS

    The enumeration's cases — .iOS, .macOS, .tvOS, and .watchOS — cover the four platforms that previews can render for. Because they are plain enum cases, you can assign, compare, and pass them like any other value, which is how platform carries the chosen target through the view.

  3. Switch over the value

    Since PreviewPlatform is an enum, you can pattern-match it with a switch to drive platform-specific behavior. The label(for:) helper switches over its platform parameter, returning a distinct string for each case so the view reflects which platform it represents.

  4. Handle unknown cases with default

    PreviewPlatform can gain cases over time, so exhaustive switch statements should include a fallback. The example closes its switch with default: return "Rendering", keeping the code valid even when a case isn't one of the four it names explicitly.

Try it — Change let platform: PreviewPlatform = .iOS to .watchOS and watch label(for:) resolve to "Rendering for watchOS".

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

PreviewPlatform.swift
struct PreviewPlatformDemo: View {
    let platform: PreviewPlatform = .iOS

    var body: some View {
        VStack(spacing: 12) {
            Image(systemName: "iphone")
                .font(.system(size: 44))
                .foregroundStyle(.blue)
            Text("Preview Platform")
                .font(.headline)
            Text(label(for: platform))
                .font(.subheadline)
                .foregroundStyle(.secondary)
        }
        .padding()
    }

    func label(for platform: PreviewPlatform) -> String {
        switch platform {
        case .iOS: return "Rendering for iOS"
        case .macOS: return "Rendering for macOS"
        case .tvOS: return "Rendering for tvOS"
        case .watchOS: return "Rendering for watchOS"
        default: return "Rendering"
        }
    }
}
Live preview
Preview Platform
Preview Platform
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →