Swift - Convert String to Int Swift Last Updated : 07 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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 initializerSwift 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: 0Using NSStringNSString 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 Create Quiz Comment B bhuwanesh Follow 0 Improve B bhuwanesh Follow 0 Improve Article Tags : Swift Explore Swift IntroductionSwift - Hello World Program2 min readSwift - Basic Syntax5 min readSwift - Keywords4 min readSwift - Literals10 min readSwift - Constants, Variables & Print function2 min readSwift - Operators8 min readSwift Data TypesSwift - Data Types5 min readSwift - Integer, Floating-Point Numbers2 min readStrings in Swift8 min readString Functions and Operators in Swift10 min readHow to Concatenate Strings in Swift?3 min readHow to Append a String to Another String in Swift?2 min readHow to Insert a Character in String at Specific Index in Swift?2 min readHow to check if a string contains another string in Swift?2 min readHow to remove a specific character from a string in Swift?2 min readData Type Conversions in Swift5 min readSwift - Convert String to Int Swift3 min readSwift Control FlowSwift - Decision Making Statements5 min readSwift - If Statement3 min readSwift - If-else Statement3 min readSwift - If-else-if Statement3 min readSwift - Nested if-else Statement4 min readSwift - Switch Statement10 min readSwift - Loops5 min readSwift - While Loop2 min readSwift - Repeat While loop3 min readSwift - For-in Loop6 min readSwift - Control Statements in Loops5 min readSwift - Break Statement6 min readSwift - Fallthrough Statement4 min readSwift - Guard Statement15 min readSwift FunctionsCalling Functions in Swift6 min readSwift Function Parameters and Return Values10 min readHow to Use Variadic Parameters in Swift?6 min readSwift - In Out Parameters4 min readSwift - Nested Function5 min readSwift - Function Overloading8 min readClosures in Swift9 min readEscaping and Non-Escaping Closures in Swift5 min readHigher-Order Functions in Swift10 min readSwift CollectionsSwift - Arrays9 min readSwift - Arrays Properties4 min readSwift - How to Store Values in Arrays?4 min readHow to Remove the last element from an Array in Swift?4 min readHow to Remove the First element from an Array in Swift?4 min readHow to count the elements of an Array in Swift?2 min readHow to Reverse an Array in Swift?3 min readSwift Array joined() function3 min readHow to check if the array contains a given element in Swift?3 min readSorting an Array in Swift5 min readHow to Swap Array items in Swift?2 min readHow to check if an array is empty in Swift?2 min readSwift - Sets15+ min readSwift - Set Operations9 min readHow to remove first element from the Set in Swift?2 min readHow to remove all the elements from the Set in Swift?2 min readHow to check if the set contains a given element in Swift?3 min readHow to count the elements of a Set in Swift?2 min readSorting a Set in Swift4 min readHow to check if a set is empty in Swift?2 min readHow to shuffle the elements of a set in Swift?2 min readSwift - Difference Between Sets and Arrays3 min readSwift - Dictionary9 min readSwift - Tuples3 min readSwift - Iterate Arrays and Dictionaries6 min readSwift OOPsSwift Structures7 min readSwift - Properties and its Different Types13 min readSwift - Methods5 min readSwift - Difference Between Function and Method4 min readSwift - Deinitialization and How its Works?4 min readTypecasting in Swift7 min readRepeating Timers in Swift4 min readNon Repeating Timers in Swift7 min readDifference between Repeating and Non-Repeating timers in Swift4 min readOptional Chaining in Swift9 min readSingleton Class in Swift4 min readSwift Additional TopicsSwift - Error Handling2 min readDifference between Try, Try?, and Try! in Swift4 min readSwift - Typealias7 min readImportant Points to Know About Java and Swift3 min readDifference between Swift Structures and C Structure4 min readHow to Build and Publish SCADE Apps to Apple and Google Stores?11 min read6 Best iOS Project Ideas For Beginners8 min read Like