How to debug your SwiftUI previews in Xcode
March 24, 2020
With SwiftUI and Xcode 11, it’s much faster to create great user interfaces for apps on iOS, macOS, watchOS, and tvOS. As you write your app, use Xcode‘s design tools at any point to see live previews of your app in different states and on different compatible devices — all without launching Simulator.
And here’s a valuable tip: We’re going to break down how you can debug that SwiftUI code live in Xcode, too.
Enable Debug Preview in Xcode 11
When creating an interface for your app in SwiftUI, you‘ll see a preview inside Xcode that displays your app, formatted for your currently-selected device.
If you’re hitting an issue in your code, however, and don’t know where to troubleshoot, you can instead enable Debug Preview to quickly find that pesky bug. Here’s how to do it.
- Open your Swift project in Xcode 11.
- Right-click (or Control-click) on the Live Preview button in the bottom right corner of the preview.
- Select Debug Preview.
Now, you can troubleshoot your SwiftUI project using all of the usual debugging tools, such as breakpoints, view debugging, memory graph debugging, and logging to the Console.