Print in Swift 3

情到浓时终转凉″ 提交于 2019-12-22 03:59:22

问题


i would like to know what's the different between these two way to print the object in Swift. The result seems identical.

var myName : String = "yohoo" 
print ("My name is \(myName).")

print ("My name is ", myName, ".")

回答1:


There is almost no functional difference, the comma simply inputs a space either before or after the string.

let name = "John"

// both print "Hello John"
print("Hello", name)
print("Hello \(name)")



回答2:


You can use the \(variable) syntax to create interpolated strings, which are then printed just as you input them. However, the print(var1,var2) syntax has some "facilities":

  • It automatically adds a space in between each two variables, and that is called separator
  • You can customise your separator based on the context, for example:

    var hello = "Hello"
    var world = "World!"
    print(hello,world,separator: "|")    // prints "Hello|World!"
    print(hello,world,separator: "\\//")    // prints "Hello\\//World!"
    


来源:https://stackoverflow.com/questions/43963356/print-in-swift-3

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