简体   繁体   中英

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

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.

This works

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM