在 Swift 中使用 println
和 print
都会打印到控制台。但它们之间的唯一区别似乎是 println
返回到下一行,而 print
不会。
例如:
println("hello world")
println("another world")
会输出以下两行:
hello world
another world
同时:
print("hello")
print("world")
只输出一行:
helloworld
print
似乎更像是 C 中传统的 printf
。Swift 文档指出 println
等同于 NSLog
但是 print
的目的是什么,除了不返回到下一行之外还有什么理由使用它吗?
最佳答案
在新的 swift 2 中,println 已重命名为 print which 作为选项“终止符”参数。
(更新于 2015 年 9 月 16 日,使用新的终止符:“”)
var fruits = ["banana","orange","cherry"]
// #1
for f in fruits{
print(f)
}
// #2
for f in fruits{
print("\(f) ", terminator: "")
}
#1 将打印
banana
orange
cherry
#2 将打印
banana orange cherry
关于swift - Swift 中 println 和 print 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24072775/