String interpolation is just several imagine term for combining variables and constants within a string.
For example a string like so:
var brand = "Apple"
Now we might simply print this out by executing:
print("My favorite brand is \(brand)")
We might also utilize this way:
print("My favorite brand is " + brand)
But this technique isn’t very efficient especially if your combining lots of different data types.
Let’s execute an example with various data types:
var brand = "Apple" var month = "may" var day = 1 var year = 1987 print("\(brand) was started on \(month) \(day), \(year)")
Use math
You can simply use math within String Interpolation.
var brand = "Apple" var month = "may" var day = 1 var year = 1987 print("\(brand) was started on \(month) \(day * 10), \(year / 5)")
Leave a Reply