Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Swift – Convert String to Int Swift

  • Last Updated : 07 Jan, 2022

Swift provides a number of ways to convert a string value into an integer value. Though, we can convert a numeric string only into an integer value. In this article, we will see the two most common methods used to convert a string to an integer value. A string is a collection of characters. Swift provides String data type with the help of which we can store a string. For example, “GeeksforGeeks”, “GFG”, etc. A variable can be declared as String type by following the below syntax,

Syntax:

let myVariable: String 

For example: myVariable = “GeeksforGeeks”

Using Int initializer

Swift provides the function of integer initializers using which we can convert a string into an Int type. To handle non-numeric strings, we can use nil coalescing using which the integer initializer returns an optional integer. 

let myStringVariable = “25”

let myIntegerVariable = Int(myStringVariable) ?? 0                       

It means that if the string is non-numeric then assign the variable with 0 otherwise convert the numeric string to an integer. Below is the implementation to convert a String into Int:

Example:

Swift




// Swift program to convert String to Int  
// Here we are converting a numeric string.
  
// Initializing a constant variable of string type
let myStringVariable = "25"
  
// Converting the string into integer type
let myIntegerVariable = Int(myStringVariable) ?? 0
  
// Print the value represented by myIntegerVariable
print("Integer Value:", myIntegerVariable)
  
// Here, we are converting a non-numeric string.
// Initializing a constant variable of string type
let myStringvariable = "GeeksforGeeks"
  
// Trying to convert "GeeksforGeeks" 
// to the integer equivalent but since 
// it's non-numeric string hence the
// optional value would be assigned 
// to the integer variable which is
// equal to 0 in this case
let myIntegervariable = Int(myStringvariable) ?? 0
  
// Print the value represented by myIntegervariable
print("Integer Value:", myIntegervariable)

Output:

Integer Value: 25
Integer Value: 0

Using NSString

NSString in Swift is a class that is used to create objects that lie in heap and they are passed by reference. It provides different types of methods for comparing, searching, and modifying strings. We can convert a numeric string into an integer value indirectly. Firstly, we can convert a string into an NSString then we can use “integerValue” property used with NSStrings to convert an NSString into an integer value. 

Example: 

In the below program we have converted the numeric string into NSString then we have applied “integerValue” property to convert a string into an integer value.

Swift




// Swift program to convert String to Int  
import Foundation
import Glibc
  
let myString = "25"
  
// Firstly we are converting a string into NSString then
// using integerValue property we get integer value
let myIntegerVariable = (myString as NSString) .integerValue  
print("Integer Value:", myString)

Output:

Integer Value: 25

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!