简体   繁体   English

单击按钮时(不在模拟器中),如何在 XCode 12.3“预览”中的 SwiftUI 中打印内容?

[英]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.我正在尝试使用 SwiftUI(不是模拟器)在 Xcode 12.3 中使用“预览”在调试控制台中打印一些文本,但我遇到了问题......似乎没有任何显示。 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.我还尝试了在单独的帖子中提出的一种方法,即在体外打印 function,但它也不起作用,简单的文本也不会打印到控制台。 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")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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