2016-06-12 131 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