Your First iOS and SwiftUI App · Type Inference |

This is a companion discussion topic for the original entry at

Hey there,

Working my way through this excellent course, I’m a bit stuck on the following, with using the slider values :
Coming from AppKit, I was used to the « sliderValueChanged » method in which we could just code what to do when the slider was moved. For example, simply print out to the console its value !

I’m trying to print the slider’s value in the console, but I can’t seem to be able to do this. Where should I place the
print(“the slider value is : (self.sliderValue)”) Code ?

I am able to display the slider’s value in a text, dynamically. I’m just wondering if I can print to the console.


@esowes Printing to the console in SwiftUI is actually not as straightforward and out of the box after all and does not work in the same way as it certainly does in UIKit unfortunately, so you definitely have to find workarounds for it in this case instead. First create an extension for the view where you want to print from to keep everything nice and clean like this:

extension PrintView {
  func printValue(_ value: String) -> AnyView {
    return AnyView(EmptyView())

printValue(_: ) prints a String to the console and returns an empty view which is type erased so you can actually return it from the body of your custom view after all. Now you can definitely print anything to the console from your view as follows:

struct PrintView: View {
  var body: AnyView {
    printValue("Printing to the console in SwiftUI")

Please let me know if you have any questions or issues about the whole thing when you actually get a chance after all and we can definitely talk even more about everything. Thank you for posting the question - much appreciated! I have really learned a lot about how SwiftUI actually works under the hood after all just by finding the best possible way to solve this.

Wow Cosmin,

Thanks a lot for going so deep into providing an answer for me ! I really appreciate it.
SwiftUI sure is in a weird state right now… Text fields are also very poorly supported as far as first responder etc are concerned…

Best regards,

Serge Ostrowsky

1 Like