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.
Choose a platform case
PreviewPlatformexposes one case per supported device family, and you select the target by referring to a case directly. The example stores a fixed choice inlet platform: PreviewPlatform = .iOS, declaring that this view is meant to be seen as it appears on iOS.Use
.iOS,.macOS,.tvOS, and.watchOSThe 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 howplatformcarries the chosen target through the view.Switch over the value
Since
PreviewPlatformis anenum, you can pattern-match it with aswitchto drive platform-specific behavior. Thelabel(for:)helper switches over itsplatformparameter, returning a distinct string for each case so the view reflects which platform it represents.Handle unknown cases with
defaultPreviewPlatformcan gain cases over time, so exhaustiveswitchstatements should include a fallback. The example closes its switch withdefault: return "Rendering", keeping the code valid even when a case isn't one of the four it names explicitly.
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.
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"
}
}
}