swiftprinting

Print in Swift 3


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, ".")

Solution

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

    This is because the default separator argument used by print is a space " ". You can customize this.

    When you use a string interpolation (like "\(name)") you are directly specifying the format yourself.

    let name = "John"
    
    // all print "Hello John"
    print("Hello", name)
    print("Hello", name, separator: " ")
    print("Hello \(name)")