Swift convert strings into different data types: Easy guide

As a software developer, you often encounter scenarios where you need to convert data from one type to another, and string conversion is a fundamental skill to master. 

In this blog post, we’ll explore the art of converting strings into different data types like int, date, double and you will also learn how to convert your strings and prevent your app from crashing if it’s a wrong data type.

How to convert string to int in Swift?

One of the most frequent string conversions is turning a string containing numeric characters into an integer variable.

In Swift, you can convert a string to an integer using the Int initializer. We will wrap our conversion in an if let, so if the conversion fails our app won’t crash:

let stringWithInt = "42"

if let integerValue = Int(stringWithInt) {
    print("Success, you have created a integer: \(integerValue)")
} else {
    print("Conversion failed. The string is NOT a integer.")
}

Swift convert string to float

As in the example above with converting a string to an int, we will convert our string to a float in the same way — we will make an if let that makes sure our app won’t crash and then to the conversion.

In the following example, we will convert a string to a float:

let stringWithFloat = "42.0"

if let floatValue = Float(stringWithFloat) {
    print("Success, you have created a float: \(floatValue)")
} else {
    print("Conversion failed. The string is NOT a float.")
}

Swift convert string to double

To convert a string to a double, we will do it in the same way as the above two examples, so first we will have our string containing a double value and then wrap the conversion in an if let.

In the following example, we will convert a string into a double:

let stringWithDouble = "42.0"

if let doubleValue = Float(stringWithDouble) {
    print("Success, you have created a double: \(doubleValue)")
} else {
    print("Conversion failed. The string is NOT a double.")
}

How to convert string into data type?

Apple has made it easy for us to create a data type from a string, they have created a initializer for us to use. To create a data type from a string you will need to use the .data initializer.

In the following example, we will convert a string variable to data:

let stringConvertToData = "Convert me to data"

if let dataFromString = stringConvertToData.data(using: .utf8) {
    print("Converted String to Data:", dataFromString)
} else {
    print("Failed to convert String to Data")
}

Keep in mind that the choice of encoding (.utf8 in this case) depends on the character set of your string. If your string uses a different encoding, you should choose the appropriate encoding option.

Swift convert string to array

If you want to convert a Swift string into an array of substrings, you can simply initiate an array with a string and then you have converted your string into an array.

let stringConvertToArray = "Array me 😀"

let stringAsArray = Array(stringConvertToArray)

Will be the following array:

["A", "r", "r", "a", "y", " ", "m", "e", " ", "😀"]

Conclusion

In this blog post we have converted strings into different data types, we have also converted them in a way your application won’t crash if the conversion fails.

I hope you enjoyed this blog post and can use it in your work.

Scroll to Top