How to print in SwiftUI Previews?

2.4k Views Asked by At

I know how to print in a SwiftUI view. I would like to do the same within a SwiftUI preview so that I don't need to run the app in the simulator to see the logs.

For example here I'm trying to print a view's frame size:

struct ContentView: View {
  var body: some View {
    VStack {
      Image(systemName: "globe")
        .imageScale(.large)
        .foregroundColor(.accentColor)
      Text("Hello, world!")
    }
    .padding()
    .background {
      GeometryReader { proxy in
        Color.clear
          .onAppear {
            print(proxy.size)
          }
      }
    }
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

This outputs properly to the Xcode Console when running the app on the simulator. However, the Xcode Console doesn't show any output when the preview is shown. I tried both "Live" and "Selectable" mode in the SwiftUI preview to no avail.


I know I can switch the print to an NSLog("SWIFTUIPREVIEWLOG: \(proxy.size)") and then search for SWIFTUIPREVIEWLOG in Console.app, but this takes longer than reading the output from the Xcode Console. Plus, the output is messier when running in the simulator.

Is there a way to view print statements originating from SwiftUI previews?

2

There are 2 best solutions below

0
On BEST ANSWER

Officially, attaching to Preview instances of your app has been deprecated. I have historically gone the Log route. Once you save a filter in Console, it can be … survivable. However, while trying to find an official source for the deprecation, I stumbled across this thread on the dev forums, and the last reply sounds hacky but interesting. As I haven’t yet had a chance to try this approach, I can’t speak to its reliability, but it seems worth a shot. (That said, you may also want to look into the Inject framework, which makes live code injection for regular simulators fairly painless and often better than SwiftUI previews.)

0
On

As of Xcode 14.3.1 There is Previews tabs in the output window that shows the previews.

If not available then there is a simple workaround for this;

  1. Create a wrapper for the view;
struct ContentViewWrapper : View {
    
    var body: some View {
        ContentView()
    }
}

Now, call this wrapper within the PreviewProvider

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentViewWrapper()
  }
}
}

Now, the prints are appearing on the console while playing with the preview.

Tested with Xcode 13.4!