Justin Bishop
Justin Bishop

Reputation: 157

Can't even get a Hello World working with SwiftUI/Xcode 12

Starting from the simple iOS App template, I make my "ContentView.swift":

import SwiftUI

struct ContentView: View {
  var body: some View {
    Button(action: {
      print("hello console!")
    }, label: {
      Text("Button")
    })
  }
}

struct ContentView_Previews: PreviewProvider {
  static var previews: some View {
    ContentView()
  }
}

I start up a preview in Debug Preview, I activate the console so it shows up below, I see my button that says "Button". I click the button on the text that says "Button", but still see nothing. What am I missing??

Upvotes: 0

Views: 538

Answers (1)

pawello2222
pawello2222

Reputation: 54601

You need to enable Debug Preview.

From SwiftUI tips and tricks:

If you press play in the SwiftUI preview to try out your designs, you’ll find that any calls to print() are ignored. If you’re using print() for testing purposes – e.g. as simple button tap actions – then this can be a real headache.

Fortunately, there’s a simple fix: right-click on the play button in the preview canvas and choose “Debug Preview”. With that small change made you’ll find your print() calls work as normal.

Upvotes: 1

Related Questions