enum to String conversion

In the example code in this thread: https://stackoverflow.com/questions/61235935/understanding-some-nwbrowser-i-managed-to-get-working there is this line:

print("result ", result )

The second argument in the print statement, the "result" variable, is not a String type. It is instead a struct whose definition begins in line 10731 in the "Network" import file. Somehow the print function knows how to convert this into a string for printing to the terminal. I conclude, with some uncertainty, that the instructions for doing this conversion have to be defined in this struct. Where in this struct is the conversion defined? If not defined there, how is the conversion done?

Welcome to the forum.

Instead of referring to line 10371 of some code, please copy this part of code and show the result you get in the log as well as the struct definition (you speak of an enum we don't see anywhere). That will help give you a more precise answer.

But print is flexible enough to print other things than just String.

For instance, if you have a struct defined as:

struct Dummy {
  var a: Int 
  var b: String 
}

Then

let dummy = Dummy(a: 1, b: "Hello")
print("The struct", dummy)

will give something like

TheStruct Dummy(a: 10, b: "Hello")

If that's not clear enough, please explain what you don't catch. Otherwise, don't forget to close the thread.

Somehow the print function knows how to convert this into a string for printing to the terminal.

When you print something it renders it to a string in one of two ways [1]:

  • If the type conforms to CustomStringConvertible, it calls that to render it to a string.

  • If not, it has its own built in logic.

Your result value is of type NWBrowser.Result, right? That type doesn’t conform to CustomStringConvertible, so the built-in logic kicks in. NWBrowser.Result is a struct, and the result you’re seeing is how print(…) renders a struct by default.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] If you call debugPrint(…) there’s an extra step, namely to go through CustomDebugStringConvertible.

enum to String conversion
 
 
Q