Interpolation is a powerful feature in Swift that enables us to embed expressions inside a string literal. This way, we can dynamically include variables, constants, expressions, and functions right inside a string without having to concatenate them.
Here's an example:
let name = "Alice"
let age = 27
let message = "Hello, my name is \(name) and I'm \(age) years old."
print(message) // Output: Hello, my name is Alice and I'm 27 years old.
In this code, we're assigning the values "Alice" and 27 to the name and age variables respectively. Then, using string interpolation, we embed those values into the message string using the \( ) syntax. The resulting message is then printed to the console.
We can also use expressions inside the interpolation block. For example:
let x = 10
let y = 5
let add = "\(x) + \(y) = \(x + y)"
let subtract = "\(x) - \(y) = \(x - y)"
print(add) // Output: 10 + 5 = 15
print(subtract) // Output: 10 - 5 = 5
Here, we're using the + and - operators inside the interpolation blocks to dynamically calculate the values of add and subtract.
Interpolation is a very useful feature that can save time, improve readability, and help to avoid errors caused by incorrectly concatenating strings and variables.