Blog

OpenSwiftUI Project

  • OS: AppleOS

Preview OpenSwiftUI views directly in Xcode

OpenSwiftUI 0.21.0 adds an OpenSwiftUI-aware #Preview macro, removing the hosting-controller wrapper from the everyday preview workflow on supported Apple platforms.

OpenSwiftUI 0.21.0 closes a visible gap in the development loop: an OpenSwiftUI view can now appear directly inside an Xcode #Preview declaration. The source-level interface is the familiar one, whether the package is built from source or consumed through the matching OpenSwiftUI-spm binary distribution.

Earlier Preview support proved that Xcode could render OpenSwiftUI content, but each declaration had to turn its view into a platform view controller with _previewVC(). Version 0.21.0 gives OpenSwiftUI its own Preview macro overload and moves that bridge behind the declaration developers write.

Preview support

Import OpenSwiftUI and place the view directly in the Preview body. An optional name can still identify the preview in Xcode.

import OpenSwiftUI

#Preview("Content") {
    ContentView()
}

Why OpenSwiftUI-spm carries macro sources

The OpenSwiftUI source package contains both sides of its macros. The OpenSwiftUI module contains declarations such as the new Preview macro, while the OpenSwiftUIMacros target contains the compiler plugin that implements their expansions. In a source build, SwiftPM sees that dependency and builds the plugin for the development host.

The binary package has to assemble those pieces differently. OpenSwiftUI-spm consumes OpenSwiftUI as target-platform XCFrameworks, but a compiler macro runs on the build host and cannot be supplied by those framework binaries. The release workflow therefore mirrors the matching Sources/OpenSwiftUIMacros directory into OpenSwiftUI-spm. SwiftPM builds that source as a host macro plugin while the application links the prebuilt OpenSwiftUI frameworks.

This duplication is packaging glue, not an extra integration step for applications. The OpenSwiftUI product in OpenSwiftUI-spm already includes the binary targets and the OpenSwiftUIMacros target, so consumers only depend on that one product. Keeping the mirrored source aligned with the framework release ensures that the macro declarations in the binary module and their implementations on the host stay in sync.

Only integrations that consume the XCFrameworks directly, without OpenSwiftUI-spm, need to provide the matching macro plugin themselves. Without it, they can still use the earlier ContentView()._previewVC() form to select the platform controller Preview macro.