How to print something in SwiftUI in XCode 12.3 "Preview" when a button is clicked (not in Simulator)?

Viewed 1605

I am trying to print some text in the debug console using the "Preview" in Xcode 12.3 using SwiftUI (not the simulator), and am having issues... nothing appears to show up. I tried right clicking and hitting "Debug Preview" in the preview area also and nothing happens.

I also tried a method proposed in a separate post by having a Print function outside of the body, but it also does not work and simple text does not get printed to the console. My code is as follows:

import SwiftUI

extension View {
    func Print(_ vars: Any...) -> some View {
        for v in vars { print(v) }
        return EmptyView()
    }
}

struct ContentView: View {

var body: some View {
VStack {
                        Button(action: {
print("Hello") // does not get printed
                        Print("Hello") // does not get printed
}) {
Text("Click Me")
}
}

}

}

If there is no way to get this to work in preview and printing to console only works when the Simulator is running, that is an acceptable answer, but if at all possible I'd like to be able to have print statements to the console without running simulator for ease of not having to wait for the simulator to load.

1 Answers

This works

Button(action: { print("Hello") }) {
    Text("Press Button")
}
Related