I can't understand how string interpolation works in Swift 3. Here are two variables, optional x
and implicitly unwrapped optional y
:
let x: Int? = 1
let y: Int! = 2
Printing of this two optionals looks quite logic:
print(x) // Optional(1)
print(y) // 2
But why string interpolation works in other way?
print("x: \(x)") // x: Optional(1)
print("y: \(y)") // y: Optional(2)
Why should I unwrap already unwrapped optional?
print("y: \(y!)") // y: 2
Let's assume here was used CustomStringConvertible
protocol that uses description
property to convert Int!
to String
. But why here's no y: Optional(2)
?
print("y: \(y!.description)") // y: 2
print("y: \(y?.description)") // y: Optional("2")
print("y: \(y.description)") // y: 2
Could anyone please explain that?