Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot print to console in Xcode

I am new to Xcode, macOS development etc.. So maybe its just because I am new - but I could not make a simple printout to console work with all effort.

I created a minimum nonworking example of my problem:

import SwiftUI

struct Test: View {
    @State var message = "Test"
    var body: some View {
        Button(action: {
            print("test worked")
            message = "test worked"
       }) {
            Text(message)
       }
    }
}

struct Test_Previews: PreviewProvider {
    static var previews: some View {
        Test()
    }
}

When executing this the Button text changes, but nothing appears in console.

I tried by following this tutorial, but it did not work with it either: https://www.hackingwithswift.com/read/18/2/basic-swift-debugging-using-print

I tried by enabling debug preview according to this thread: How to print() to Xcode console in SwiftUI?

And I tried by enabling this setting: enter image description here

Nothing helps.. I am using Xcode Version 12.1 (12A7403) btw.

like image 372
tbrodbeck Avatar asked Oct 18 '25 05:10

tbrodbeck


2 Answers

I have faced the same problem nothing shows in console.This helps me. Make sure you mark right side button to see the debug console.

enter image description here

like image 61
Solayman Rana Avatar answered Oct 19 '25 18:10

Solayman Rana


You cannot print to the console from a SwiftUI preview.

The only possibility for outputting debug info in a preview is to display your logs in a Text (or any other UI element) that's displayed in your Preview.

However, if you need proper debugging, run the full app, don't use previews. Previews are great for initial wireframes, but once you get to the stage where you need debugging, switch to using the view in your app and running that on the Simulator (or a real device) rather than using the preview.

like image 24
Dávid Pásztor Avatar answered Oct 19 '25 20:10

Dávid Pásztor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!