How to print in SwiftUI Previews?
Asked Answered
S

2

4

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?

Solubilize answered 23/12, 2022 at 16:50 Comment(0)
A
1

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.)

Acervate answered 23/12, 2022 at 22:23 Comment(0)
R
3

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!

Rainbolt answered 5/5, 2023 at 18:53 Comment(0)
A
1

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.)

Acervate answered 23/12, 2022 at 22:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.