In Swift, you can convert an integer to a string using the String()
initializer or string interpolation.
In this example,
num
and assign it a value of 42
.strNum
using the String()
initializer with the integer num
.String()
initializer converts the integer to its string representation.// Convert Integer to String using String() Initializer
let num = 42
let strNum = String(num)
print("String representation of integer: \(strNum)")
String representation of integer: 42
In this example,
negativeNum
with a value of -10
.negativeNum
to a string and store the result in the constant strNegativeNum
.// Convert Negative Integer to String using String Interpolation
let negativeNum = -10
let strNegativeNum = "String representation of negative integer: \(negativeNum)"
print(strNegativeNum)
String representation of negative integer: -10
In this tutorial, we learned How to Convert Integer to String in Swift language with well detailed examples.