Difference between println and print in Swift

别说谁变了你拦得住时间么 提交于 2019-11-30 19:44:21

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 

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

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) ")
}

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!