When running a preview on an app target, the target is used as the preview's host. When running a preview on non-app targets, Xcode is using XCPreviewAgent as the host. The problem is that XCPreviewAgent process does not allow debugging. But we can workaround this.
The XCPreviewAgent is just a dummy iOS app without any logic, so we can create our own app with same name and bundle identifier, build it with debug capabilities and install it on the simulator used for running the preview. To do that, first install the app on normal simulator by running the app, then copy the bundle to the preview simulator (you can find the path by checking the issue report in Xcode's preview canvas). After that we will be able to attach debugger to now our custom XCPreviewAgent and debug our previews.
I'm using this hack to develop SwifUI views in isolation as a separate Swift Package without a real need for a project file. Running SwiftUI previews on the main app is very inefficient as the whole project has to be rebuilt every time you run a preview.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: