Convert a String to an Integer in Swift

In this Swift programming tutorial, you will learn how to convert a string to an integer.

In the field of app development, you often need to convert a string into an integer so that you can use it in some calculations. So let’s see how we can perform this task with the help of some simple examples.

In this tutorial, you will see two different methods that will help for converting a Swift string to an integer, one is by using the integer initializer and the other is by using the NumberFormatter.

Convert to an Int using the integer initializer

If a string consists of a valid integer, then you can use the integer initializer to convert it to an integer. Below is the how we can do it:

let myStr = "765"

let myInt = Int(myStr)

In the above code, the myInt variable will be an integer that has the value 765.

Valid string format

Off course, the Swift string should be in valid format if we want to convert it to an integer. There are only a few possibilities that the string will be valid. The string can starts with “-” or “+” sign at the beginning followed by digits and also can be consists of digits without the “-” or “+” sign.

Int("765")
// 765

Int("+765")
// 765

Int("-765")
// -765

Invalid string format

Swift strings that can not be converted into integers are invalid strings. If you try to convert it to an integer, it will be a nil value.

let myString = "765M"

In the above example, the string is invalid. If we try to create an integer from this string, it will be nil. Below is the list of some invalid strings:

Int(" 765")                       // Includes whitespace
Int("34-63")                      // Invalid string format
Int("cc5500")                     // Characters out of bounds
Int("10000000000000000000000000") // Out of range
Int("untitled876")                // Characters out of bounds
Int("765untitled")                // Characters out of bounds
Int("7,876")                      // Characters out of bounds

Although, you can force a value to be an integer by using an optional. See the example below:

let myString = "876H"

let myInt = Int(myString) ?? 0

In the above example, the string value will be 0 instead of nil because we force it to become 0 in the absence of the value.

String to integer conversion using Swift NumberFormatter

You can also use the Swift NumberFormatter to create an int from a given string. Well, this is not a simple way as you see in our previous examples where we used an integer initializer. Below is how we can use the NumberFormatter to convert our Swift string into an integer:

import Foundation

let nf = NumberFormatter()
let myIntValue = nf.number(from: "987")?.intValue
/// 987

Below is another example with whitespace in the string at the beginning and ending opf the string:

let nf = NumberFormatter()
let myIntValue = nf.number(from: " 987 ")?.intValue
/// 987

In the above example, it will not become an invalid string even though there is whitespace at the starting and ending. But if we use the integer identifier, it will be then nil. This is one of the differences between using an integer initializer and NumberFormatter.

Conclusion

In this tutorial, we have learned how to perform string to integer conversion in Swift programming using integer initializer and using NumberFormatter. The first one where we are using an integer initializer is a quite simple way for this task. But it depends on you which one is more convenient for your project.

I hope, this tutorial will help you in your development work or also if you are a beginner.

Leave a Reply

Your email address will not be published.