Concatenate number with string in Swift

Begum picture Begum · Jul 9, 2014 · Viewed 50.7k times · Source

I need to concatenate a String and Int as below:

let myVariable: Int = 8
return "first " + myVariable

But it does not compile, with the error:

Binary operator '+' cannot be applied to operands of type 'String' and 'Int'

What is the proper way to concatenate a String + Int?

Answer

jtbandes picture jtbandes · Jul 9, 2014

If you want to put a number inside a string, you can just use String Interpolation:

return "first \(myVariable)"