Convert an Integer to a String in Swift


In this tutorial, you will learn how to convert an integer value to a string in Swift programming. For example, you may need to convert an integer 321 to a string "321".

In order to convert a Swift integer to a string containing the digits of our specified integer, we can use the String() initializer. This is the simplest way to perform this task. You have to pass the integer value to the String() initializer as a parameter. Then it will return a string value that exactly contains the digits of our specified integer in the same order.

Yes, it’s that easy. You can do it just by a single line of code.

Below is given the syntax for using the String() for converting our integer to a string:

String(INTEGER_VALUE)

In the above syntax INTEGER_VALUE is the integer that we want to transform into a string.

Now let’s see an example of the Swift program:

var intVal :Int = 321
var strVal :String = String(intVal)

In the above Swift program, we have declared an integer-type variable intVal and assigned the integer value 321 in it. In the next line, we have created a variable of type string and stored the string value that is converted from the integer intVal.

You can see that we have passed our integer intVal to the String initializer. As we previously mentioned, it returns a string formed using the digits of the integer in the same order.