2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
tbrodbeck
  • 460
  • 1
  • 8
  • 16
  • 2
    Are you running the app or just using SwiftUI preview? SwiftUI preview won't print to the console. – Dávid Pásztor Nov 04 '20 at 10:33
  • 1
    Yes I am using the preview. Okay this is the reason for the issue. Is there a possibility to display/log something from the preview? – tbrodbeck Nov 04 '20 at 12:01

4 Answers4

5

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

Solayman Rana
  • 283
  • 3
  • 12
4

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.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
  • 1
    where did you get this information? It would be very stupid for Apple to intentionally give us the "preview debug" functionality without logging. I'd like to think it's just a bug. – onthemoon Nov 09 '20 at 17:47
  • 1
    @onthemoon sadly this is not documented. It might be a bug, but I highly doubt it is, I think this is simply how Apple envisioned SwiftUI previews to work. To be honest, if you are able to create a preview with dummy data for your view, it's really easy to simply instantiate the view the same way in your `SceneDelegate`, from where you can use the debugger to its full extent. So if you really need full debugging behaviour, just plug your view in your `SceneDelegate` and run the app. – Dávid Pásztor Nov 09 '20 at 17:53
  • 1
    Actually running a simulator worked for me, after trying so many things for two hours. Gosh. Thanks for this answer. –  Apr 16 '21 at 16:26
2

It's possible to debug SwiftUI previews and print in the console without launching the app on a device or simulator. From the canvas, make sure to click on "Debug preview". More info can be found on Apple website.

Xcode 12

Long press on the Live Preview button, then click on Debug Preview.

enter image description here


Xcode 11

Right-click (or Control-click) on the Live Preview button in the bottom right corner of the preview.

enter image description here

alpennec
  • 1,864
  • 3
  • 18
  • 25
0

I'm also having the same problem. I think it's a bug. I am pretty sure it was working on some previous xcode versions. Anyway as a workaround you can just set a breakpoint on the line where you 'd like to log something and manually set a debugger command action like you can see in the picture.

Don't forget to tick the checkbox if you want to just print the log without actually stopping.

enter image description here

onthemoon
  • 3,302
  • 3
  • 17
  • 24