尝试打印两个值时出错

时间:2016-06-12 19:28:35

标签: swift printing

official swift document中说 " print(_:separator:terminator:)函数是一个全局函数,可以将一个或更多值打印到适当的输出。"

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

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

问题作为代码中的注释 - 为什么print(friendlyWelcom, welcomeMessage)print(friendlyWelcom, separator: ",", terminator: ""有效但print(friendlyWelcom, separator: ",", terminator: "", welcomeMessage, separator: ",", terminator: "")会产生编译错误?

1 个答案:

答案 0 :(得分:2)

您不能简单地添加命名参数。相反,您应该将变量传递给以逗号分隔的第一个参数。然后它们与中间的分隔符和末尾的终结符连接:

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

输出

  

您好! - 您好?

您可以根据需要添加任意数量的变量:

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

您好! - 你好 - 123 - somethingElse - etc !!!!