How to debug your SwiftUI previews in Xcode

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.

  1. Open your Swift project in Xcode 11.
  2. Right-click (or Control-click) on the Live Preview button in the bottom right corner of the preview.
  3. Select Debug Preview.

The Debug Preview mode in Xcode provides easy access to an assortment of debugging tools for your app, including a visual breakdown of your app’s elements.

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.

Resources

Learn more about SwiftUI

Check out Apple’s SwiftUI Tutorials

Check out Mastering SwiftUI Previews