Difference between println and print in Swift

2019-02-12 22:47发布

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?

标签: swift println
5条回答
再贱就再见
2楼-- · 2019-02-12 22:59

The deference between print and println is that after print prints the cursor does not skip lines and after println prints the cursor skips a line

查看更多
来,给爷笑一个
3楼-- · 2019-02-12 23:00

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

查看更多
smile是对你的礼貌
4楼-- · 2019-02-12 23:01

It is same as in Java print is just print where ln in println means "Next Line". It will create a next line for you.

查看更多
贼婆χ
5楼-- · 2019-02-12 23:05

Exactly like you said, to print without adding a new line. There are some cases where you may want this. This is a simple example:

var arr = [1,2,3,4,5]

print("My array contains: ")
for num in arr{
    print("\(num) ")
}
查看更多
你好瞎i
6楼-- · 2019-02-12 23:06

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 
查看更多
登录 后发表回答