Adarsh Yadav
Adarsh Yadav

Reputation: 21

Difference between Swift String Interpolation Prints

I am new to iOS development, while I was going through string interpolation. I want to know the clarification between these print statement's output:

var value = "5"
print("Values is: \(value)")
print("Values is:", value)
print("Values is: " + value)

Output is : Values is: 5
Values is: 5
Values is: 5

Upvotes: 1

Views: 371

Answers (4)

Ahmad F
Ahmad F

Reputation: 31645

For such a question, we should take a look at print(_:separator:terminator:) parameters:

1) items: is a variadic parameter of type Any, which means that you can pass zero or more items to it. Example:

print() // passing nothing
print("Hello") // passing single item (String)
print(101, 40.45, false, ["Hi", "Greetings"]) // passing multiple items

2) separator: the string to print between each item (as mentioned in its documentation). Example:

print(101, 40.45, false, ["Hi", "Greetings"], separator: " <=> ")
// 101<=>40.45<=>false<=>["Hi", "Greetings"]

3) terminator: the string to print after all items have been printed (as mentioned in its documentation). Example:

print(101, 40.45, false, ["Hi", "Greetings"], terminator: " ==>")
// 101 40.45 false ["Hi", "Greetings"] ==>

Back to your cases:

First, keep in mind that for all of your three cases you are passing only items parameter; It is valid -for sure- because separator and terminator have default values as " " and \n.

Now, for the first and third print statements

print("Values is: \(value)")
print("Values is: " + value)

what happens is: actually you are dealing with Strings, it is not about the print itself. You can do interpolation in strings as well as using the + for concatenating strings without the print:

// interpolation:
let name = "Jack"
let greetingMessage = "Greetings, \(name)"
print(greetingMessage) // => Greetings, Jack

// concatenating:
let concatenated = "Greetings" + ", " + "Sara"
print(concatenated) // => "Greetings" + ", " + "Sara"

Which means that you are passing a single String item, regardless of doing interpolation or concatenation for it.

You could also check The + function implementation in Swift. Basically, it is an append!


The second print statement:

print("Values is:", value)

What happens here is: you are passing two items; According to the default value for separator, the output is:

Values is: 5

As:

Values is: 5
     ^    ^^
     |    ||__ item #2
 item #1  |
          |
   default separator (" ")

Upvotes: 0

iOS Developer
iOS Developer

Reputation: 470

var value = "5"

print("Values is: (value)") // Print the value as a part of the the string. If you use print("Values is:(value)"), it will print the output without space.

print("Values is:", value) // you do not need to add a separate space to add the value to sting. It will automatically add the value to the string with a space.

print("Values is: " + value) // It will show error if you use integer value "Binary operator '+' cannot be applied to operands of type 'String' and 'Int'"

otherwise it will work. And if you want to concatenate int with sting you need to do something like below:-

print("Values is: " + String(value)) // it is normal concatenate number with string

All the above will print the exact

Upvotes: 0

AtulParmar
AtulParmar

Reputation: 4570

In this print statement output is the same but there is different like in first statement use \(value) variable within the string data.

The second statement append data in your string value with keep one space

The third statement just concat two value (it does not keep space between two value), In this statement "+" sign used as operator overloading to concat two value

let value = "5"
print("Values is: \(value)") //use variable value within string
print("Values is:", value)   //append value, with keep one space
print("Values is: " + value) //just concat two value 

Upvotes: 0

vadian
vadian

Reputation: 285092

Practically all three forms do the same thing.

The differences are

  1. String interpolation syntax. You can put everything within the inner parentheses which responds to the CustomStringConvertible protocol.
  2. Variadic parameter syntax. print is declared func print(_ items: Any...,. Any... means you can pass multiple items comma separated which are treated as array.
  3. String concatenation syntax : The strings are concatenated with the + operator

If 5 was Int rather than String forms 1 and 2 are valid but not form 3

Upvotes: 2

Related Questions