Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between println and print in Swift

Tags:

swift

println

The use of println and print in Swift both print to the console. But the only difference between them seems to be that println returns to the next line whereas print will not.

For example:

println("hello world")
println("another world")

will output the following two lines:

hello world
another world

while:

print("hello")
print("world")

outputs only one line:

helloworld

The print seems to be more like the traditional printf in C. The Swift documentation states that println is the equivalent to NSLog but what's the purpose of print, is there any reason to use it other than not returning to the next line?

like image 470
wigging Avatar asked Jun 06 '14 01:06

wigging


2 Answers

In the new swift 2, the println has been renamed to print which as an option "terminator" argument.

(udpated 2015-09-16 with the new terminator: "")

var fruits = ["banana","orange","cherry"]

// #1
for f in fruits{
    print(f)
}

// #2
for f in fruits{
    print("\(f) ", terminator: "")
}

#1 will print

banana
orange
cherry

#2 will print

banana orange cherry 
like image 153
Jeremy Chone Avatar answered Oct 22 '22 21:10

Jeremy Chone


That's exactly what it is, it's used when you want to print multiple things on the same line.

like image 39
Lorenzo Avatar answered Oct 22 '22 21:10

Lorenzo