2016-06-12 98 views
1

在它说 的official swift documentprint(_: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: "")生成编译器错误?

回答

2

您不能随意添加命名参数。相反,您应该将变量作为第一个参数分隔,并以逗号分隔。然后,他们得到与加盟之间的隔膜和终止底:

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

输出

您好! - 你好?

可以有添加尽可能多的变量,如你所愿:

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

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

+0

感谢您的解释,现在对我来说这很清楚。 – SLN