SLN SLN - 5 months ago 26
Swift Question

error when try to print two values

In the official swift document it says
"The

print(_:separator:terminator:)
function is a global function that prints one or more values to an appropriate output."

var welcomeMessage = "Hello"
var friendlyWelcom = "Hello!"

print(friendlyWelcom, separator: ",", terminator: "", welcomeMessage, separator: ",", terminator: "") // Why this is not working


Question As the comments inside the code why
print(friendlyWelcom, welcomeMessage)
and
print(friendlyWelcom, separator: ",", terminator: ""
is working but
print(friendlyWelcom, separator: ",", terminator: "", welcomeMessage, separator: ",", terminator: "")
has an compile-time error?

Thanks a lot for your help and time

Answer

You cannot simply add named parameters as you wish. Instead you should pass the variables to print in comma seperated as the first argument. They then get joined with the separator in between and the terminator at the end:

print(friendlyWelcom, welcomeMessage, separator: " - ", terminator: "?")

Outputs

Hello! - Hello?

You can add as many variables there as you wish:

print(friendlyWelcom, welcomeMessage, 123, "somethingElse", "etc", separator: " - ", terminator: "!!!!")

Hello! - Hello - 123 - somethingElse - etc!!!!