What is this “terminator” for in this for--in?

var namesAndScores = ["Anna": 2, "Brian": 2, "Craig": 8, "Donna": 6]
for player in namesAndScores.keys {
print("\(player), ",  terminator:"")
}
print("")

The above is in the Swift Apprentice book. The result is four names. What is “terminator” for?

The default behaviour of print is to output the string argument to the console, followed by a newline character. The result is that every print statement takes up a whole number of lines, and the next print statement starts from the next line. The terminator is the newline character at the end that isn’t part of your string, but is printed at the end.

You might want a different terminator for some things. The terminator could be the empty string. It could be a full stop (period) at the end of a sentence. It could be multiple newline characters if you want some blank lines afterward.

1 Like