Converting String to Int with Swift












304















The application basically calculates acceleration by inputting Initial and final velocity and time and then use a formula to calculate acceleration. However, since the values in the text boxes are string, I am unable to convert them to integers.



@IBOutlet var txtBox1 : UITextField
@IBOutlet var txtBox2 : UITextField
@IBOutlet var txtBox3 : UITextField
@IBOutlet var lblAnswer : UILabel


@IBAction func btn1(sender : AnyObject) {

let answer1 = "The acceleration is"
var answer2 = txtBox1
var answer3 = txtBox2
var answer4 = txtBox3









share|improve this question




















  • 6





    Haven't tried but maybe you could cast the values like var answer1 = Int(txtBox1.text)

    – Daniel
    Jun 9 '14 at 7:10











  • If you string is suppose "23.0", then if you cast it to Int("23.0") it will return nil, for this case you first need to cast to Double/Float and then again cast to Int.

    – Ariven Nadar
    Nov 29 '18 at 14:00


















304















The application basically calculates acceleration by inputting Initial and final velocity and time and then use a formula to calculate acceleration. However, since the values in the text boxes are string, I am unable to convert them to integers.



@IBOutlet var txtBox1 : UITextField
@IBOutlet var txtBox2 : UITextField
@IBOutlet var txtBox3 : UITextField
@IBOutlet var lblAnswer : UILabel


@IBAction func btn1(sender : AnyObject) {

let answer1 = "The acceleration is"
var answer2 = txtBox1
var answer3 = txtBox2
var answer4 = txtBox3









share|improve this question




















  • 6





    Haven't tried but maybe you could cast the values like var answer1 = Int(txtBox1.text)

    – Daniel
    Jun 9 '14 at 7:10











  • If you string is suppose "23.0", then if you cast it to Int("23.0") it will return nil, for this case you first need to cast to Double/Float and then again cast to Int.

    – Ariven Nadar
    Nov 29 '18 at 14:00
















304












304








304


43






The application basically calculates acceleration by inputting Initial and final velocity and time and then use a formula to calculate acceleration. However, since the values in the text boxes are string, I am unable to convert them to integers.



@IBOutlet var txtBox1 : UITextField
@IBOutlet var txtBox2 : UITextField
@IBOutlet var txtBox3 : UITextField
@IBOutlet var lblAnswer : UILabel


@IBAction func btn1(sender : AnyObject) {

let answer1 = "The acceleration is"
var answer2 = txtBox1
var answer3 = txtBox2
var answer4 = txtBox3









share|improve this question
















The application basically calculates acceleration by inputting Initial and final velocity and time and then use a formula to calculate acceleration. However, since the values in the text boxes are string, I am unable to convert them to integers.



@IBOutlet var txtBox1 : UITextField
@IBOutlet var txtBox2 : UITextField
@IBOutlet var txtBox3 : UITextField
@IBOutlet var lblAnswer : UILabel


@IBAction func btn1(sender : AnyObject) {

let answer1 = "The acceleration is"
var answer2 = txtBox1
var answer3 = txtBox2
var answer4 = txtBox3






ios swift int uitextfield






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 22 '18 at 10:34









ayaio

58.3k20132189




58.3k20132189










asked Jun 9 '14 at 6:57









Marwan QasemMarwan Qasem

1,526274




1,526274








  • 6





    Haven't tried but maybe you could cast the values like var answer1 = Int(txtBox1.text)

    – Daniel
    Jun 9 '14 at 7:10











  • If you string is suppose "23.0", then if you cast it to Int("23.0") it will return nil, for this case you first need to cast to Double/Float and then again cast to Int.

    – Ariven Nadar
    Nov 29 '18 at 14:00
















  • 6





    Haven't tried but maybe you could cast the values like var answer1 = Int(txtBox1.text)

    – Daniel
    Jun 9 '14 at 7:10











  • If you string is suppose "23.0", then if you cast it to Int("23.0") it will return nil, for this case you first need to cast to Double/Float and then again cast to Int.

    – Ariven Nadar
    Nov 29 '18 at 14:00










6




6





Haven't tried but maybe you could cast the values like var answer1 = Int(txtBox1.text)

– Daniel
Jun 9 '14 at 7:10





Haven't tried but maybe you could cast the values like var answer1 = Int(txtBox1.text)

– Daniel
Jun 9 '14 at 7:10













If you string is suppose "23.0", then if you cast it to Int("23.0") it will return nil, for this case you first need to cast to Double/Float and then again cast to Int.

– Ariven Nadar
Nov 29 '18 at 14:00







If you string is suppose "23.0", then if you cast it to Int("23.0") it will return nil, for this case you first need to cast to Double/Float and then again cast to Int.

– Ariven Nadar
Nov 29 '18 at 14:00














28 Answers
28






active

oldest

votes


















262














Basic Idea, note that this only works in Swift 1.x (check out ParaSara's answer to see how it works in Swift 2.x):



    // toInt returns optional that's why we used a:Int?
let a:Int? = firstText.text.toInt() // firstText is UITextField
let b:Int? = secondText.text.toInt() // secondText is UITextField

// check a and b before unwrapping using !
if a && b {
var ans = a! + b!
answerLabel.text = "Answer is (ans)" // answerLabel ie UILabel
} else {
answerLabel.text = "Input values are not numeric"
}


Update for Swift 4



...
let a:Int? = Int(firstText.text) // firstText is UITextField
let b:Int? = Int(secondText.text) // secondText is UITextField
...





share|improve this answer


























  • Thanks this works. However, I have an issue since I want the numbers to include floats too. Thanks again.

    – Marwan Qasem
    Jun 12 '14 at 18:15








  • 3





    If you need floats (and you really really want Double, not float), toInt() will not do it. Could you use your imagination and the available documentation to find a suitable function?

    – gnasher729
    Jun 13 '14 at 7:18






  • 4





    I get 'NSString' does not have a member named 'toInt'. Any Ideas?

    – code ninja
    Nov 4 '14 at 11:16






  • 1





    NSString and String are two different objects and have different methods. NSString has a method called .intValue

    – Byron Coetsee
    Jan 22 '15 at 12:24






  • 6





    This solution was only right for Swift and not for Swift2. Now you should use: Int(firstText.text)

    – gurehbgui
    Sep 27 '15 at 10:36



















331














Update Answer for swift 2.0 :



toInt() method is given a error. Because,In Swift 2.x, the .toInt() function was removed from String. In replacement, Int now has an initializer that accepts a String:



let a:Int? = Int(firstText.text)     // firstText is UITextField  
let b:Int? = Int(secondText.text) // secondText is UITextField





share|improve this answer


























  • Can i ask why i am getting an error when i omit the '?' char? Why do i need to state 'a' as an optional?

    – Manos Serifios
    Jun 2 '16 at 20:16








  • 1





    @ManosSerifios this discussion may helpful : stackoverflow.com/questions/32621022/…

    – Paraneetharan Saravanaperumal
    Jun 3 '16 at 5:36






  • 1





    @ParaSara Thanks a lot. This make it clear to me.

    – Manos Serifios
    Jun 3 '16 at 9:55











  • not truly related but the constructor approach is always preferred and more readable for Int to Strings. "(someInt)" is not good String(someInt) is much easier to read

    – Honey
    Sep 30 '16 at 16:09











  • I am printing Int(firstText.text)! and then I still see optional. Why? Have I not unwrapped it?

    – Honey
    Nov 22 '16 at 17:50



















78














myString.toInt() - convert the string value into int .



Swift 3.x



If you have an integer hiding inside a string, you can convertby using the integer's constructor, like this:



let myInt = Int(textField.text)


As with other data types (Float and Double) you can also convert by using NSString:



let myString = "556"
let myInt = (myString as NSString).integerValue





share|improve this answer





















  • 1





    this actually answers the question, all of the others tell the OP how to coast an Integer as a String, which is not what he was asking

    – aremvee
    Jul 8 '15 at 12:19






  • 1





    Example for Swift 3?

    – Peter Kreinz
    Nov 3 '16 at 10:18











  • pls clarify "latest Swift Versions" for posterity's hoped lack of confusion :)

    – Alex Hall
    Jul 3 '17 at 21:44











  • @aremvee do you mean "cast" an integer as a string? And what exactly does this do that answers the question which the other answers don't?

    – Alex Hall
    Jul 3 '17 at 21:46



















31














Xcode 8.3.2 • Swift 3.1



class IntegerField: UITextField {
var integer: Int {
return string.digits.integer
}
override func willMove(toSuperview newSuperview: UIView?) {
addTarget(self, action: #selector(editingChanged), for: .editingChanged)
keyboardType = .numberPad
textAlignment = .right
editingChanged()
}
func editingChanged() {
text = Formatter.decimal.string(for: integer)
print(integer)
}
}




Required extensions, structs and initializers:



extension Sequence where Iterator.Element == UnicodeScalar {
var string: String { return String(String.UnicodeScalarView(self)) }
}

extension Formatter {
static let decimal = NumberFormatter(numberStyle: .decimal)
}

extension UITextField {
var string: String { return text ?? "" }
}

extension String {
private static var digitsPattern = UnicodeScalar("0")..."9"
var digits: String {
return unicodeScalars.filter { String.digitsPattern ~= $0 }.string
}
var integer: Int { return Int(self) ?? 0 }
}

extension NumberFormatter {
convenience init(numberStyle: Style) {
self.init()
self.numberStyle = numberStyle
}
}





share|improve this answer

































    22














    You wanna use NSNumberFormatter().numberFromString(yourNumberString). It's great because it returns an an optional that you can then test with an "if let" to determine if the conversion was successful.
    eg.



         var myString = "(10)"
    if let myNumber = NSNumberFormatter().numberFromString(myString) {
    var myInt = myNumber.integerValue
    // do what you need to do with myInt
    } else {
    // what ever error code you need to write
    }





    share|improve this answer





















    • 1





      I just changed it to 'myNumber.integerValue' since Xcode 7 won't build with 'intValue'. The latter is of Int32 value

      – brainray
      Jun 9 '15 at 11:39



















    17














    swift 4.0



    let stringNumber = "123"
    let number = Int(stringNumber) //here number is of type "Int?"


    //using Forced Unwrapping

    if number != nil {
    //string is converted to Int
    }


    you could also use Optional Binding other than forced binding.



    eg:



      if let number = Int(stringNumber) { 
    // number is of type Int
    }





    share|improve this answer































      14














      //Xcode 8.1 and swift 3.0



      We can also handle it by Optional Binding, Simply



      let occur = "10"

      if let occ = Int(occur) {
      print("By optional binding :", occ*2) // 20

      }





      share|improve this answer

































        7














        Swift 3



        The simplest and more secure way is:



        @IBOutlet var textFieldA  : UITextField
        @IBOutlet var textFieldB : UITextField
        @IBOutlet var answerLabel : UILabel

        @IBAction func calculate(sender : AnyObject) {

        if let intValueA = Int(textFieldA),
        let intValueB = Int(textFieldB) {
        let result = intValueA + intValueB
        answerLabel.text = "The acceleration is (result)"
        }
        else {
        answerLabel.text = "The value (intValueA) and/or (intValueB) are not a valid integer value"
        }
        }


        Avoid invalid values setting keyboard type to number pad:



         textFieldA.keyboardType = .numberPad
        textFieldB.keyboardType = .numberPad





        share|improve this answer

































          5














          In Swift 4:



          extension String {            
          var numberValue:NSNumber? {
          let formatter = NumberFormatter()
          formatter.numberStyle = .decimal
          return formatter.number(from: self)
          }
          }
          let someFloat = "12".numberValue





          share|improve this answer

































            4














            i have made a simple program, where you have 2 txt field you take input form the user and add them to make it simpler to understand please find the code below.



            @IBOutlet weak var result: UILabel!
            @IBOutlet weak var one: UITextField!
            @IBOutlet weak var two: UITextField!

            @IBAction func add(sender: AnyObject) {
            let count = Int(one.text!)
            let cal = Int(two.text!)
            let sum = count! + cal!
            result.text = "Sum is (sum)"
            }


            hope this helps.






            share|improve this answer

































              3














              About int() and Swift 2.x: if you get a nil value after conversion check if you try to convert a string with a big number (for example: 1073741824), in this case try:



              let bytesInternet : Int64 = Int64(bytesInternetString)!





              share|improve this answer



















              • 1





                Thank you this worked for my case. Int() was working for me with 16 digit numbers but recently started to fail.

                – Ryan Boyd
                Apr 30 '16 at 7:03



















              3














              Latest swift3 this code is simply to convert string to int



              let myString = "556"
              let myInt = Int(myString)





              share|improve this answer

































                3














                Swift 3.0



                Try this, you don't need to check for any condition I have done everything just use this function. Send anything string, number, float, double ,etc,. you get a number as a value or 0 if it is unable to convert your value



                Function:



                func getNumber(number: Any?) -> NSNumber {
                guard let statusNumber:NSNumber = number as? NSNumber else
                {
                guard let statString:String = number as? String else
                {
                return 0
                }
                if let myInteger = Int(statString)
                {
                return NSNumber(value:myInteger)
                }
                else{
                return 0
                }
                }
                return statusNumber
                }


                Usage:
                Add the above function in code and to convert use
                let myNumber = getNumber(number: myString)
                if the myString has a number or string it returns the number else it returns 0



                Example 1:



                let number:String = "9834"
                print("printing number (getNumber(number: number))")


                Output: printing number 9834



                Example 2:



                let number:Double = 9834
                print("printing number (getNumber(number: number))")


                Output: printing number 9834



                Example 3:



                let number = 9834
                print("printing number (getNumber(number: number))")


                Output: printing number 9834






                share|improve this answer

































                  3














                  In Swift 4.2 and Xcode 10.1



                  let string:String = "789"
                  let intValue:Int = Int(string)!
                  print(intValue)

                  let integerValue:Int = 789
                  let stringValue:String = String(integerValue)
                  //OR
                  //let stringValue:String = "(integerValue)"
                  print(stringValue)





                  share|improve this answer

































                    2














                    Because a string might contain non-numerical characters you should use a guard to protect the operation. Example:



                    guard let labelInt:Int = Int(labelString) else {
                    return
                    }

                    useLabelInt()





                    share|improve this answer































                      1














                      Use this:



                      // get the values from text boxes
                      let a:Double = firstText.text.bridgeToObjectiveC().doubleValue
                      let b:Double = secondText.text.bridgeToObjectiveC().doubleValue

                      // we checking against 0.0, because above function return 0.0 if it gets failed to convert
                      if (a != 0.0) && (b != 0.0) {
                      var ans = a + b
                      answerLabel.text = "Answer is (ans)"
                      } else {
                      answerLabel.text = "Input values are not numberic"
                      }


                      OR



                      Make your UITextField KeyboardType as DecimalTab from your XIB or storyboard, and remove any if condition for doing any calculation, ie.



                      var ans = a + b
                      answerLabel.text = "Answer is (ans)"


                      Because keyboard type is DecimalPad there is no chance to enter other 0-9 or .



                      Hope this help !!






                      share|improve this answer

































                        1














                        //  To convert user input (i.e string) to int for calculation.I did this , and it works.


                        let num:Int? = Int(firstTextField.text!);

                        let sum:Int = num!-2

                        print(sum);





                        share|improve this answer

































                          1














                          This works for me



                          var a:Int? = Int(userInput.text!)





                          share|improve this answer


























                          • How is this different from the solution given in the comment?

                            – Prune
                            Oct 15 '15 at 22:25






                          • 2





                            The solution given in the comment is missing "!" at the end , which is expected in Swift 2 and onwards

                            – Naishta
                            Oct 22 '15 at 20:45



















                          1














                          for Swift3.x



                          extension String {
                          func toInt(defaultValue: Int) -> Int {
                          if let n = Int(self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)) {
                          return n
                          } else {
                          return defaultValue
                          }
                          }
                          }





                          share|improve this answer































                            1














                            In Swift 2.x, the .toInt() function was removed from String. In replacement, Int now has an initializer that accepts a String



                            Int(myString)



                            In your case, you could use Int(textField.text!) insted of textField.text!.toInt()



                            Swift 1.x



                            let myString: String = "256"
                            let myInt: Int? = myString.toInt()


                            Swift 2.x, 3.x



                            let myString: String = "256"
                            let myInt: Int? = Int(myString)





                            share|improve this answer































                              0














                              for Alternative solution. You can use extension a native type. You can test with playground.



                              extension String {
                              func add(a: Int) -> Int? {
                              if let b = Int(self) {
                              return b + a
                              }
                              else {
                              return nil
                              }
                              }
                              }


                              "2".add(1)






                              share|improve this answer































                                0














                                My solution is to have a general extension for string to int conversion.



                                extension String {

                                // default: it is a number suitable for your project if the string is not an integer

                                func toInt(default: Int) -> Int {
                                if let result = Int(self) {
                                return result
                                }
                                else {
                                return default
                                }
                                }

                                }





                                share|improve this answer

































                                  0














                                  @IBAction func calculateAclr(_ sender: Any) {
                                  if let addition = addition(arrayString: [txtBox1.text, txtBox2.text, txtBox3.text]) {
                                  print("Answer = (addition)")
                                  lblAnswer.text = "(addition)"
                                  }
                                  }

                                  func addition(arrayString: [Any?]) -> Int? {

                                  var answer:Int?
                                  for arrayElement in arrayString {
                                  if let stringValue = arrayElement, let intValue = Int(stringValue) {
                                  answer = (answer ?? 0) + intValue
                                  }
                                  }

                                  return answer
                                  }





                                  share|improve this answer

































                                    0














                                    As of swift 3, I have to force my #%@! string & int with a "!" otherwise it just doesn't work.



                                    For example:



                                    let prefs = UserDefaults.standard
                                    var counter: String!
                                    counter = prefs.string(forKey:"counter")
                                    print("counter: (counter!)")


                                    var counterInt = Int(counter!)
                                    counterInt = counterInt! + 1
                                    print("counterInt: (counterInt!)")

                                    OUTPUT:
                                    counter: 1
                                    counterInt: 2





                                    share|improve this answer
























                                    • Can't you just do var counterInt = counter.map { Int($0) } ? Where counter whould be a String?

                                      – Martin
                                      Oct 19 '17 at 16:34











                                    • @Martin - No. ? makes its optional and thus adds the word "optional" to the counter string.

                                      – Sam B
                                      Oct 19 '17 at 22:27











                                    • IMHO, you should not force unwrap your optionals. Prefer use guard and if let statements

                                      – Martin
                                      Oct 20 '17 at 7:56



















                                    0














                                    Question : string "4.0000" can not be convert into integer using Int("4.000")?



                                    Answer : Int() check string is integer or not if yes then give you integer and otherwise nil. but Float or Double can convert any number string to respective Float or Double without giving nil. Example if you have "45" integer string but using Float("45") gives you 45.0 float value or using Double("4567") gives you 45.0.



                                    Solution : NSString(string: "45.000").integerValue or Int(Float("45.000")!)! to get correct result.






                                    share|improve this answer































                                      0














                                      I recently got the same issue. Below solution is work for me:



                                              let strValue = "123"
                                      let result = (strValue as NSString).integerValue





                                      share|improve this answer































                                        0














                                        An Int in Swift contains an initializer that accepts a String. It returns an optional Int? as the conversion can fail if the string contains not a number.



                                        By using an if let statement you can validate whether the conversion succeeded.



                                        So your code become something like this:



                                        @IBOutlet var txtBox1 : UITextField
                                        @IBOutlet var txtBox2 : UITextField
                                        @IBOutlet var txtBox3 : UITextField
                                        @IBOutlet var lblAnswer : UILabel

                                        @IBAction func btn1(sender : AnyObject) {

                                        let answer1 = "The acceleration is"
                                        var answer2 = txtBox1
                                        var answer3 = txtBox2
                                        var answer4 = txtBox3

                                        if let intAnswer = Int(txtBox1.text) {
                                        // Correctly converted
                                        }
                                        }





                                        share|improve this answer































                                          -1














                                          Convert String value to Integer in Swift 4



                                          let strValue:String = "100"
                                          let intValue = strValue as! Int
                                          var intValueFromString:Int = strValue as! Int
                                          or
                                          var intValueFromString = Int(strValue)!





                                          share|improve this answer

























                                            Your Answer






                                            StackExchange.ifUsing("editor", function () {
                                            StackExchange.using("externalEditor", function () {
                                            StackExchange.using("snippets", function () {
                                            StackExchange.snippets.init();
                                            });
                                            });
                                            }, "code-snippets");

                                            StackExchange.ready(function() {
                                            var channelOptions = {
                                            tags: "".split(" "),
                                            id: "1"
                                            };
                                            initTagRenderer("".split(" "), "".split(" "), channelOptions);

                                            StackExchange.using("externalEditor", function() {
                                            // Have to fire editor after snippets, if snippets enabled
                                            if (StackExchange.settings.snippets.snippetsEnabled) {
                                            StackExchange.using("snippets", function() {
                                            createEditor();
                                            });
                                            }
                                            else {
                                            createEditor();
                                            }
                                            });

                                            function createEditor() {
                                            StackExchange.prepareEditor({
                                            heartbeatType: 'answer',
                                            autoActivateHeartbeat: false,
                                            convertImagesToLinks: true,
                                            noModals: true,
                                            showLowRepImageUploadWarning: true,
                                            reputationToPostImages: 10,
                                            bindNavPrevention: true,
                                            postfix: "",
                                            imageUploader: {
                                            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                                            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                                            allowUrls: true
                                            },
                                            onDemand: true,
                                            discardSelector: ".discard-answer"
                                            ,immediatelyShowMarkdownHelp:true
                                            });


                                            }
                                            });














                                            draft saved

                                            draft discarded


















                                            StackExchange.ready(
                                            function () {
                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24115141%2fconverting-string-to-int-with-swift%23new-answer', 'question_page');
                                            }
                                            );

                                            Post as a guest















                                            Required, but never shown

























                                            28 Answers
                                            28






                                            active

                                            oldest

                                            votes








                                            28 Answers
                                            28






                                            active

                                            oldest

                                            votes









                                            active

                                            oldest

                                            votes






                                            active

                                            oldest

                                            votes









                                            262














                                            Basic Idea, note that this only works in Swift 1.x (check out ParaSara's answer to see how it works in Swift 2.x):



                                                // toInt returns optional that's why we used a:Int?
                                            let a:Int? = firstText.text.toInt() // firstText is UITextField
                                            let b:Int? = secondText.text.toInt() // secondText is UITextField

                                            // check a and b before unwrapping using !
                                            if a && b {
                                            var ans = a! + b!
                                            answerLabel.text = "Answer is (ans)" // answerLabel ie UILabel
                                            } else {
                                            answerLabel.text = "Input values are not numeric"
                                            }


                                            Update for Swift 4



                                            ...
                                            let a:Int? = Int(firstText.text) // firstText is UITextField
                                            let b:Int? = Int(secondText.text) // secondText is UITextField
                                            ...





                                            share|improve this answer


























                                            • Thanks this works. However, I have an issue since I want the numbers to include floats too. Thanks again.

                                              – Marwan Qasem
                                              Jun 12 '14 at 18:15








                                            • 3





                                              If you need floats (and you really really want Double, not float), toInt() will not do it. Could you use your imagination and the available documentation to find a suitable function?

                                              – gnasher729
                                              Jun 13 '14 at 7:18






                                            • 4





                                              I get 'NSString' does not have a member named 'toInt'. Any Ideas?

                                              – code ninja
                                              Nov 4 '14 at 11:16






                                            • 1





                                              NSString and String are two different objects and have different methods. NSString has a method called .intValue

                                              – Byron Coetsee
                                              Jan 22 '15 at 12:24






                                            • 6





                                              This solution was only right for Swift and not for Swift2. Now you should use: Int(firstText.text)

                                              – gurehbgui
                                              Sep 27 '15 at 10:36
















                                            262














                                            Basic Idea, note that this only works in Swift 1.x (check out ParaSara's answer to see how it works in Swift 2.x):



                                                // toInt returns optional that's why we used a:Int?
                                            let a:Int? = firstText.text.toInt() // firstText is UITextField
                                            let b:Int? = secondText.text.toInt() // secondText is UITextField

                                            // check a and b before unwrapping using !
                                            if a && b {
                                            var ans = a! + b!
                                            answerLabel.text = "Answer is (ans)" // answerLabel ie UILabel
                                            } else {
                                            answerLabel.text = "Input values are not numeric"
                                            }


                                            Update for Swift 4



                                            ...
                                            let a:Int? = Int(firstText.text) // firstText is UITextField
                                            let b:Int? = Int(secondText.text) // secondText is UITextField
                                            ...





                                            share|improve this answer


























                                            • Thanks this works. However, I have an issue since I want the numbers to include floats too. Thanks again.

                                              – Marwan Qasem
                                              Jun 12 '14 at 18:15








                                            • 3





                                              If you need floats (and you really really want Double, not float), toInt() will not do it. Could you use your imagination and the available documentation to find a suitable function?

                                              – gnasher729
                                              Jun 13 '14 at 7:18






                                            • 4





                                              I get 'NSString' does not have a member named 'toInt'. Any Ideas?

                                              – code ninja
                                              Nov 4 '14 at 11:16






                                            • 1





                                              NSString and String are two different objects and have different methods. NSString has a method called .intValue

                                              – Byron Coetsee
                                              Jan 22 '15 at 12:24






                                            • 6





                                              This solution was only right for Swift and not for Swift2. Now you should use: Int(firstText.text)

                                              – gurehbgui
                                              Sep 27 '15 at 10:36














                                            262












                                            262








                                            262







                                            Basic Idea, note that this only works in Swift 1.x (check out ParaSara's answer to see how it works in Swift 2.x):



                                                // toInt returns optional that's why we used a:Int?
                                            let a:Int? = firstText.text.toInt() // firstText is UITextField
                                            let b:Int? = secondText.text.toInt() // secondText is UITextField

                                            // check a and b before unwrapping using !
                                            if a && b {
                                            var ans = a! + b!
                                            answerLabel.text = "Answer is (ans)" // answerLabel ie UILabel
                                            } else {
                                            answerLabel.text = "Input values are not numeric"
                                            }


                                            Update for Swift 4



                                            ...
                                            let a:Int? = Int(firstText.text) // firstText is UITextField
                                            let b:Int? = Int(secondText.text) // secondText is UITextField
                                            ...





                                            share|improve this answer















                                            Basic Idea, note that this only works in Swift 1.x (check out ParaSara's answer to see how it works in Swift 2.x):



                                                // toInt returns optional that's why we used a:Int?
                                            let a:Int? = firstText.text.toInt() // firstText is UITextField
                                            let b:Int? = secondText.text.toInt() // secondText is UITextField

                                            // check a and b before unwrapping using !
                                            if a && b {
                                            var ans = a! + b!
                                            answerLabel.text = "Answer is (ans)" // answerLabel ie UILabel
                                            } else {
                                            answerLabel.text = "Input values are not numeric"
                                            }


                                            Update for Swift 4



                                            ...
                                            let a:Int? = Int(firstText.text) // firstText is UITextField
                                            let b:Int? = Int(secondText.text) // secondText is UITextField
                                            ...






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Mar 7 '18 at 10:05









                                            Anton Belousov

                                            9991126




                                            9991126










                                            answered Jun 12 '14 at 11:59









                                            Narendar Singh SainiNarendar Singh Saini

                                            2,4101814




                                            2,4101814













                                            • Thanks this works. However, I have an issue since I want the numbers to include floats too. Thanks again.

                                              – Marwan Qasem
                                              Jun 12 '14 at 18:15








                                            • 3





                                              If you need floats (and you really really want Double, not float), toInt() will not do it. Could you use your imagination and the available documentation to find a suitable function?

                                              – gnasher729
                                              Jun 13 '14 at 7:18






                                            • 4





                                              I get 'NSString' does not have a member named 'toInt'. Any Ideas?

                                              – code ninja
                                              Nov 4 '14 at 11:16






                                            • 1





                                              NSString and String are two different objects and have different methods. NSString has a method called .intValue

                                              – Byron Coetsee
                                              Jan 22 '15 at 12:24






                                            • 6





                                              This solution was only right for Swift and not for Swift2. Now you should use: Int(firstText.text)

                                              – gurehbgui
                                              Sep 27 '15 at 10:36



















                                            • Thanks this works. However, I have an issue since I want the numbers to include floats too. Thanks again.

                                              – Marwan Qasem
                                              Jun 12 '14 at 18:15








                                            • 3





                                              If you need floats (and you really really want Double, not float), toInt() will not do it. Could you use your imagination and the available documentation to find a suitable function?

                                              – gnasher729
                                              Jun 13 '14 at 7:18






                                            • 4





                                              I get 'NSString' does not have a member named 'toInt'. Any Ideas?

                                              – code ninja
                                              Nov 4 '14 at 11:16






                                            • 1





                                              NSString and String are two different objects and have different methods. NSString has a method called .intValue

                                              – Byron Coetsee
                                              Jan 22 '15 at 12:24






                                            • 6





                                              This solution was only right for Swift and not for Swift2. Now you should use: Int(firstText.text)

                                              – gurehbgui
                                              Sep 27 '15 at 10:36

















                                            Thanks this works. However, I have an issue since I want the numbers to include floats too. Thanks again.

                                            – Marwan Qasem
                                            Jun 12 '14 at 18:15







                                            Thanks this works. However, I have an issue since I want the numbers to include floats too. Thanks again.

                                            – Marwan Qasem
                                            Jun 12 '14 at 18:15






                                            3




                                            3





                                            If you need floats (and you really really want Double, not float), toInt() will not do it. Could you use your imagination and the available documentation to find a suitable function?

                                            – gnasher729
                                            Jun 13 '14 at 7:18





                                            If you need floats (and you really really want Double, not float), toInt() will not do it. Could you use your imagination and the available documentation to find a suitable function?

                                            – gnasher729
                                            Jun 13 '14 at 7:18




                                            4




                                            4





                                            I get 'NSString' does not have a member named 'toInt'. Any Ideas?

                                            – code ninja
                                            Nov 4 '14 at 11:16





                                            I get 'NSString' does not have a member named 'toInt'. Any Ideas?

                                            – code ninja
                                            Nov 4 '14 at 11:16




                                            1




                                            1





                                            NSString and String are two different objects and have different methods. NSString has a method called .intValue

                                            – Byron Coetsee
                                            Jan 22 '15 at 12:24





                                            NSString and String are two different objects and have different methods. NSString has a method called .intValue

                                            – Byron Coetsee
                                            Jan 22 '15 at 12:24




                                            6




                                            6





                                            This solution was only right for Swift and not for Swift2. Now you should use: Int(firstText.text)

                                            – gurehbgui
                                            Sep 27 '15 at 10:36





                                            This solution was only right for Swift and not for Swift2. Now you should use: Int(firstText.text)

                                            – gurehbgui
                                            Sep 27 '15 at 10:36













                                            331














                                            Update Answer for swift 2.0 :



                                            toInt() method is given a error. Because,In Swift 2.x, the .toInt() function was removed from String. In replacement, Int now has an initializer that accepts a String:



                                            let a:Int? = Int(firstText.text)     // firstText is UITextField  
                                            let b:Int? = Int(secondText.text) // secondText is UITextField





                                            share|improve this answer


























                                            • Can i ask why i am getting an error when i omit the '?' char? Why do i need to state 'a' as an optional?

                                              – Manos Serifios
                                              Jun 2 '16 at 20:16








                                            • 1





                                              @ManosSerifios this discussion may helpful : stackoverflow.com/questions/32621022/…

                                              – Paraneetharan Saravanaperumal
                                              Jun 3 '16 at 5:36






                                            • 1





                                              @ParaSara Thanks a lot. This make it clear to me.

                                              – Manos Serifios
                                              Jun 3 '16 at 9:55











                                            • not truly related but the constructor approach is always preferred and more readable for Int to Strings. "(someInt)" is not good String(someInt) is much easier to read

                                              – Honey
                                              Sep 30 '16 at 16:09











                                            • I am printing Int(firstText.text)! and then I still see optional. Why? Have I not unwrapped it?

                                              – Honey
                                              Nov 22 '16 at 17:50
















                                            331














                                            Update Answer for swift 2.0 :



                                            toInt() method is given a error. Because,In Swift 2.x, the .toInt() function was removed from String. In replacement, Int now has an initializer that accepts a String:



                                            let a:Int? = Int(firstText.text)     // firstText is UITextField  
                                            let b:Int? = Int(secondText.text) // secondText is UITextField





                                            share|improve this answer


























                                            • Can i ask why i am getting an error when i omit the '?' char? Why do i need to state 'a' as an optional?

                                              – Manos Serifios
                                              Jun 2 '16 at 20:16








                                            • 1





                                              @ManosSerifios this discussion may helpful : stackoverflow.com/questions/32621022/…

                                              – Paraneetharan Saravanaperumal
                                              Jun 3 '16 at 5:36






                                            • 1





                                              @ParaSara Thanks a lot. This make it clear to me.

                                              – Manos Serifios
                                              Jun 3 '16 at 9:55











                                            • not truly related but the constructor approach is always preferred and more readable for Int to Strings. "(someInt)" is not good String(someInt) is much easier to read

                                              – Honey
                                              Sep 30 '16 at 16:09











                                            • I am printing Int(firstText.text)! and then I still see optional. Why? Have I not unwrapped it?

                                              – Honey
                                              Nov 22 '16 at 17:50














                                            331












                                            331








                                            331







                                            Update Answer for swift 2.0 :



                                            toInt() method is given a error. Because,In Swift 2.x, the .toInt() function was removed from String. In replacement, Int now has an initializer that accepts a String:



                                            let a:Int? = Int(firstText.text)     // firstText is UITextField  
                                            let b:Int? = Int(secondText.text) // secondText is UITextField





                                            share|improve this answer















                                            Update Answer for swift 2.0 :



                                            toInt() method is given a error. Because,In Swift 2.x, the .toInt() function was removed from String. In replacement, Int now has an initializer that accepts a String:



                                            let a:Int? = Int(firstText.text)     // firstText is UITextField  
                                            let b:Int? = Int(secondText.text) // secondText is UITextField






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Dec 2 '16 at 5:16









                                            rptwsthi

                                            8,56885393




                                            8,56885393










                                            answered Jun 11 '15 at 6:22









                                            Paraneetharan SaravanaperumalParaneetharan Saravanaperumal

                                            4,13011024




                                            4,13011024













                                            • Can i ask why i am getting an error when i omit the '?' char? Why do i need to state 'a' as an optional?

                                              – Manos Serifios
                                              Jun 2 '16 at 20:16








                                            • 1





                                              @ManosSerifios this discussion may helpful : stackoverflow.com/questions/32621022/…

                                              – Paraneetharan Saravanaperumal
                                              Jun 3 '16 at 5:36






                                            • 1





                                              @ParaSara Thanks a lot. This make it clear to me.

                                              – Manos Serifios
                                              Jun 3 '16 at 9:55











                                            • not truly related but the constructor approach is always preferred and more readable for Int to Strings. "(someInt)" is not good String(someInt) is much easier to read

                                              – Honey
                                              Sep 30 '16 at 16:09











                                            • I am printing Int(firstText.text)! and then I still see optional. Why? Have I not unwrapped it?

                                              – Honey
                                              Nov 22 '16 at 17:50



















                                            • Can i ask why i am getting an error when i omit the '?' char? Why do i need to state 'a' as an optional?

                                              – Manos Serifios
                                              Jun 2 '16 at 20:16








                                            • 1





                                              @ManosSerifios this discussion may helpful : stackoverflow.com/questions/32621022/…

                                              – Paraneetharan Saravanaperumal
                                              Jun 3 '16 at 5:36






                                            • 1





                                              @ParaSara Thanks a lot. This make it clear to me.

                                              – Manos Serifios
                                              Jun 3 '16 at 9:55











                                            • not truly related but the constructor approach is always preferred and more readable for Int to Strings. "(someInt)" is not good String(someInt) is much easier to read

                                              – Honey
                                              Sep 30 '16 at 16:09











                                            • I am printing Int(firstText.text)! and then I still see optional. Why? Have I not unwrapped it?

                                              – Honey
                                              Nov 22 '16 at 17:50

















                                            Can i ask why i am getting an error when i omit the '?' char? Why do i need to state 'a' as an optional?

                                            – Manos Serifios
                                            Jun 2 '16 at 20:16







                                            Can i ask why i am getting an error when i omit the '?' char? Why do i need to state 'a' as an optional?

                                            – Manos Serifios
                                            Jun 2 '16 at 20:16






                                            1




                                            1





                                            @ManosSerifios this discussion may helpful : stackoverflow.com/questions/32621022/…

                                            – Paraneetharan Saravanaperumal
                                            Jun 3 '16 at 5:36





                                            @ManosSerifios this discussion may helpful : stackoverflow.com/questions/32621022/…

                                            – Paraneetharan Saravanaperumal
                                            Jun 3 '16 at 5:36




                                            1




                                            1





                                            @ParaSara Thanks a lot. This make it clear to me.

                                            – Manos Serifios
                                            Jun 3 '16 at 9:55





                                            @ParaSara Thanks a lot. This make it clear to me.

                                            – Manos Serifios
                                            Jun 3 '16 at 9:55













                                            not truly related but the constructor approach is always preferred and more readable for Int to Strings. "(someInt)" is not good String(someInt) is much easier to read

                                            – Honey
                                            Sep 30 '16 at 16:09





                                            not truly related but the constructor approach is always preferred and more readable for Int to Strings. "(someInt)" is not good String(someInt) is much easier to read

                                            – Honey
                                            Sep 30 '16 at 16:09













                                            I am printing Int(firstText.text)! and then I still see optional. Why? Have I not unwrapped it?

                                            – Honey
                                            Nov 22 '16 at 17:50





                                            I am printing Int(firstText.text)! and then I still see optional. Why? Have I not unwrapped it?

                                            – Honey
                                            Nov 22 '16 at 17:50











                                            78














                                            myString.toInt() - convert the string value into int .



                                            Swift 3.x



                                            If you have an integer hiding inside a string, you can convertby using the integer's constructor, like this:



                                            let myInt = Int(textField.text)


                                            As with other data types (Float and Double) you can also convert by using NSString:



                                            let myString = "556"
                                            let myInt = (myString as NSString).integerValue





                                            share|improve this answer





















                                            • 1





                                              this actually answers the question, all of the others tell the OP how to coast an Integer as a String, which is not what he was asking

                                              – aremvee
                                              Jul 8 '15 at 12:19






                                            • 1





                                              Example for Swift 3?

                                              – Peter Kreinz
                                              Nov 3 '16 at 10:18











                                            • pls clarify "latest Swift Versions" for posterity's hoped lack of confusion :)

                                              – Alex Hall
                                              Jul 3 '17 at 21:44











                                            • @aremvee do you mean "cast" an integer as a string? And what exactly does this do that answers the question which the other answers don't?

                                              – Alex Hall
                                              Jul 3 '17 at 21:46
















                                            78














                                            myString.toInt() - convert the string value into int .



                                            Swift 3.x



                                            If you have an integer hiding inside a string, you can convertby using the integer's constructor, like this:



                                            let myInt = Int(textField.text)


                                            As with other data types (Float and Double) you can also convert by using NSString:



                                            let myString = "556"
                                            let myInt = (myString as NSString).integerValue





                                            share|improve this answer





















                                            • 1





                                              this actually answers the question, all of the others tell the OP how to coast an Integer as a String, which is not what he was asking

                                              – aremvee
                                              Jul 8 '15 at 12:19






                                            • 1





                                              Example for Swift 3?

                                              – Peter Kreinz
                                              Nov 3 '16 at 10:18











                                            • pls clarify "latest Swift Versions" for posterity's hoped lack of confusion :)

                                              – Alex Hall
                                              Jul 3 '17 at 21:44











                                            • @aremvee do you mean "cast" an integer as a string? And what exactly does this do that answers the question which the other answers don't?

                                              – Alex Hall
                                              Jul 3 '17 at 21:46














                                            78












                                            78








                                            78







                                            myString.toInt() - convert the string value into int .



                                            Swift 3.x



                                            If you have an integer hiding inside a string, you can convertby using the integer's constructor, like this:



                                            let myInt = Int(textField.text)


                                            As with other data types (Float and Double) you can also convert by using NSString:



                                            let myString = "556"
                                            let myInt = (myString as NSString).integerValue





                                            share|improve this answer















                                            myString.toInt() - convert the string value into int .



                                            Swift 3.x



                                            If you have an integer hiding inside a string, you can convertby using the integer's constructor, like this:



                                            let myInt = Int(textField.text)


                                            As with other data types (Float and Double) you can also convert by using NSString:



                                            let myString = "556"
                                            let myInt = (myString as NSString).integerValue






                                            share|improve this answer














                                            share|improve this answer



                                            share|improve this answer








                                            edited Jul 5 '17 at 11:41

























                                            answered Jun 9 '14 at 7:13









                                            Kumar KLKumar KL

                                            11.4k93354




                                            11.4k93354








                                            • 1





                                              this actually answers the question, all of the others tell the OP how to coast an Integer as a String, which is not what he was asking

                                              – aremvee
                                              Jul 8 '15 at 12:19






                                            • 1





                                              Example for Swift 3?

                                              – Peter Kreinz
                                              Nov 3 '16 at 10:18











                                            • pls clarify "latest Swift Versions" for posterity's hoped lack of confusion :)

                                              – Alex Hall
                                              Jul 3 '17 at 21:44











                                            • @aremvee do you mean "cast" an integer as a string? And what exactly does this do that answers the question which the other answers don't?

                                              – Alex Hall
                                              Jul 3 '17 at 21:46














                                            • 1





                                              this actually answers the question, all of the others tell the OP how to coast an Integer as a String, which is not what he was asking

                                              – aremvee
                                              Jul 8 '15 at 12:19






                                            • 1





                                              Example for Swift 3?

                                              – Peter Kreinz
                                              Nov 3 '16 at 10:18











                                            • pls clarify "latest Swift Versions" for posterity's hoped lack of confusion :)

                                              – Alex Hall
                                              Jul 3 '17 at 21:44











                                            • @aremvee do you mean "cast" an integer as a string? And what exactly does this do that answers the question which the other answers don't?

                                              – Alex Hall
                                              Jul 3 '17 at 21:46








                                            1




                                            1





                                            this actually answers the question, all of the others tell the OP how to coast an Integer as a String, which is not what he was asking

                                            – aremvee
                                            Jul 8 '15 at 12:19





                                            this actually answers the question, all of the others tell the OP how to coast an Integer as a String, which is not what he was asking

                                            – aremvee
                                            Jul 8 '15 at 12:19




                                            1




                                            1





                                            Example for Swift 3?

                                            – Peter Kreinz
                                            Nov 3 '16 at 10:18





                                            Example for Swift 3?

                                            – Peter Kreinz
                                            Nov 3 '16 at 10:18













                                            pls clarify "latest Swift Versions" for posterity's hoped lack of confusion :)

                                            – Alex Hall
                                            Jul 3 '17 at 21:44





                                            pls clarify "latest Swift Versions" for posterity's hoped lack of confusion :)

                                            – Alex Hall
                                            Jul 3 '17 at 21:44













                                            @aremvee do you mean "cast" an integer as a string? And what exactly does this do that answers the question which the other answers don't?

                                            – Alex Hall
                                            Jul 3 '17 at 21:46





                                            @aremvee do you mean "cast" an integer as a string? And what exactly does this do that answers the question which the other answers don't?

                                            – Alex Hall
                                            Jul 3 '17 at 21:46











                                            31














                                            Xcode 8.3.2 • Swift 3.1



                                            class IntegerField: UITextField {
                                            var integer: Int {
                                            return string.digits.integer
                                            }
                                            override func willMove(toSuperview newSuperview: UIView?) {
                                            addTarget(self, action: #selector(editingChanged), for: .editingChanged)
                                            keyboardType = .numberPad
                                            textAlignment = .right
                                            editingChanged()
                                            }
                                            func editingChanged() {
                                            text = Formatter.decimal.string(for: integer)
                                            print(integer)
                                            }
                                            }




                                            Required extensions, structs and initializers:



                                            extension Sequence where Iterator.Element == UnicodeScalar {
                                            var string: String { return String(String.UnicodeScalarView(self)) }
                                            }

                                            extension Formatter {
                                            static let decimal = NumberFormatter(numberStyle: .decimal)
                                            }

                                            extension UITextField {
                                            var string: String { return text ?? "" }
                                            }

                                            extension String {
                                            private static var digitsPattern = UnicodeScalar("0")..."9"
                                            var digits: String {
                                            return unicodeScalars.filter { String.digitsPattern ~= $0 }.string
                                            }
                                            var integer: Int { return Int(self) ?? 0 }
                                            }

                                            extension NumberFormatter {
                                            convenience init(numberStyle: Style) {
                                            self.init()
                                            self.numberStyle = numberStyle
                                            }
                                            }





                                            share|improve this answer






























                                              31














                                              Xcode 8.3.2 • Swift 3.1



                                              class IntegerField: UITextField {
                                              var integer: Int {
                                              return string.digits.integer
                                              }
                                              override func willMove(toSuperview newSuperview: UIView?) {
                                              addTarget(self, action: #selector(editingChanged), for: .editingChanged)
                                              keyboardType = .numberPad
                                              textAlignment = .right
                                              editingChanged()
                                              }
                                              func editingChanged() {
                                              text = Formatter.decimal.string(for: integer)
                                              print(integer)
                                              }
                                              }




                                              Required extensions, structs and initializers:



                                              extension Sequence where Iterator.Element == UnicodeScalar {
                                              var string: String { return String(String.UnicodeScalarView(self)) }
                                              }

                                              extension Formatter {
                                              static let decimal = NumberFormatter(numberStyle: .decimal)
                                              }

                                              extension UITextField {
                                              var string: String { return text ?? "" }
                                              }

                                              extension String {
                                              private static var digitsPattern = UnicodeScalar("0")..."9"
                                              var digits: String {
                                              return unicodeScalars.filter { String.digitsPattern ~= $0 }.string
                                              }
                                              var integer: Int { return Int(self) ?? 0 }
                                              }

                                              extension NumberFormatter {
                                              convenience init(numberStyle: Style) {
                                              self.init()
                                              self.numberStyle = numberStyle
                                              }
                                              }





                                              share|improve this answer




























                                                31












                                                31








                                                31







                                                Xcode 8.3.2 • Swift 3.1



                                                class IntegerField: UITextField {
                                                var integer: Int {
                                                return string.digits.integer
                                                }
                                                override func willMove(toSuperview newSuperview: UIView?) {
                                                addTarget(self, action: #selector(editingChanged), for: .editingChanged)
                                                keyboardType = .numberPad
                                                textAlignment = .right
                                                editingChanged()
                                                }
                                                func editingChanged() {
                                                text = Formatter.decimal.string(for: integer)
                                                print(integer)
                                                }
                                                }




                                                Required extensions, structs and initializers:



                                                extension Sequence where Iterator.Element == UnicodeScalar {
                                                var string: String { return String(String.UnicodeScalarView(self)) }
                                                }

                                                extension Formatter {
                                                static let decimal = NumberFormatter(numberStyle: .decimal)
                                                }

                                                extension UITextField {
                                                var string: String { return text ?? "" }
                                                }

                                                extension String {
                                                private static var digitsPattern = UnicodeScalar("0")..."9"
                                                var digits: String {
                                                return unicodeScalars.filter { String.digitsPattern ~= $0 }.string
                                                }
                                                var integer: Int { return Int(self) ?? 0 }
                                                }

                                                extension NumberFormatter {
                                                convenience init(numberStyle: Style) {
                                                self.init()
                                                self.numberStyle = numberStyle
                                                }
                                                }





                                                share|improve this answer















                                                Xcode 8.3.2 • Swift 3.1



                                                class IntegerField: UITextField {
                                                var integer: Int {
                                                return string.digits.integer
                                                }
                                                override func willMove(toSuperview newSuperview: UIView?) {
                                                addTarget(self, action: #selector(editingChanged), for: .editingChanged)
                                                keyboardType = .numberPad
                                                textAlignment = .right
                                                editingChanged()
                                                }
                                                func editingChanged() {
                                                text = Formatter.decimal.string(for: integer)
                                                print(integer)
                                                }
                                                }




                                                Required extensions, structs and initializers:



                                                extension Sequence where Iterator.Element == UnicodeScalar {
                                                var string: String { return String(String.UnicodeScalarView(self)) }
                                                }

                                                extension Formatter {
                                                static let decimal = NumberFormatter(numberStyle: .decimal)
                                                }

                                                extension UITextField {
                                                var string: String { return text ?? "" }
                                                }

                                                extension String {
                                                private static var digitsPattern = UnicodeScalar("0")..."9"
                                                var digits: String {
                                                return unicodeScalars.filter { String.digitsPattern ~= $0 }.string
                                                }
                                                var integer: Int { return Int(self) ?? 0 }
                                                }

                                                extension NumberFormatter {
                                                convenience init(numberStyle: Style) {
                                                self.init()
                                                self.numberStyle = numberStyle
                                                }
                                                }






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Oct 23 '17 at 17:56

























                                                answered Dec 15 '15 at 16:39









                                                Leo DabusLeo Dabus

                                                135k32277351




                                                135k32277351























                                                    22














                                                    You wanna use NSNumberFormatter().numberFromString(yourNumberString). It's great because it returns an an optional that you can then test with an "if let" to determine if the conversion was successful.
                                                    eg.



                                                         var myString = "(10)"
                                                    if let myNumber = NSNumberFormatter().numberFromString(myString) {
                                                    var myInt = myNumber.integerValue
                                                    // do what you need to do with myInt
                                                    } else {
                                                    // what ever error code you need to write
                                                    }





                                                    share|improve this answer





















                                                    • 1





                                                      I just changed it to 'myNumber.integerValue' since Xcode 7 won't build with 'intValue'. The latter is of Int32 value

                                                      – brainray
                                                      Jun 9 '15 at 11:39
















                                                    22














                                                    You wanna use NSNumberFormatter().numberFromString(yourNumberString). It's great because it returns an an optional that you can then test with an "if let" to determine if the conversion was successful.
                                                    eg.



                                                         var myString = "(10)"
                                                    if let myNumber = NSNumberFormatter().numberFromString(myString) {
                                                    var myInt = myNumber.integerValue
                                                    // do what you need to do with myInt
                                                    } else {
                                                    // what ever error code you need to write
                                                    }





                                                    share|improve this answer





















                                                    • 1





                                                      I just changed it to 'myNumber.integerValue' since Xcode 7 won't build with 'intValue'. The latter is of Int32 value

                                                      – brainray
                                                      Jun 9 '15 at 11:39














                                                    22












                                                    22








                                                    22







                                                    You wanna use NSNumberFormatter().numberFromString(yourNumberString). It's great because it returns an an optional that you can then test with an "if let" to determine if the conversion was successful.
                                                    eg.



                                                         var myString = "(10)"
                                                    if let myNumber = NSNumberFormatter().numberFromString(myString) {
                                                    var myInt = myNumber.integerValue
                                                    // do what you need to do with myInt
                                                    } else {
                                                    // what ever error code you need to write
                                                    }





                                                    share|improve this answer















                                                    You wanna use NSNumberFormatter().numberFromString(yourNumberString). It's great because it returns an an optional that you can then test with an "if let" to determine if the conversion was successful.
                                                    eg.



                                                         var myString = "(10)"
                                                    if let myNumber = NSNumberFormatter().numberFromString(myString) {
                                                    var myInt = myNumber.integerValue
                                                    // do what you need to do with myInt
                                                    } else {
                                                    // what ever error code you need to write
                                                    }






                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Jun 9 '15 at 11:38









                                                    brainray

                                                    7,353852102




                                                    7,353852102










                                                    answered Feb 27 '15 at 14:03









                                                    Abaho KatabarwaAbaho Katabarwa

                                                    22922




                                                    22922








                                                    • 1





                                                      I just changed it to 'myNumber.integerValue' since Xcode 7 won't build with 'intValue'. The latter is of Int32 value

                                                      – brainray
                                                      Jun 9 '15 at 11:39














                                                    • 1





                                                      I just changed it to 'myNumber.integerValue' since Xcode 7 won't build with 'intValue'. The latter is of Int32 value

                                                      – brainray
                                                      Jun 9 '15 at 11:39








                                                    1




                                                    1





                                                    I just changed it to 'myNumber.integerValue' since Xcode 7 won't build with 'intValue'. The latter is of Int32 value

                                                    – brainray
                                                    Jun 9 '15 at 11:39





                                                    I just changed it to 'myNumber.integerValue' since Xcode 7 won't build with 'intValue'. The latter is of Int32 value

                                                    – brainray
                                                    Jun 9 '15 at 11:39











                                                    17














                                                    swift 4.0



                                                    let stringNumber = "123"
                                                    let number = Int(stringNumber) //here number is of type "Int?"


                                                    //using Forced Unwrapping

                                                    if number != nil {
                                                    //string is converted to Int
                                                    }


                                                    you could also use Optional Binding other than forced binding.



                                                    eg:



                                                      if let number = Int(stringNumber) { 
                                                    // number is of type Int
                                                    }





                                                    share|improve this answer




























                                                      17














                                                      swift 4.0



                                                      let stringNumber = "123"
                                                      let number = Int(stringNumber) //here number is of type "Int?"


                                                      //using Forced Unwrapping

                                                      if number != nil {
                                                      //string is converted to Int
                                                      }


                                                      you could also use Optional Binding other than forced binding.



                                                      eg:



                                                        if let number = Int(stringNumber) { 
                                                      // number is of type Int
                                                      }





                                                      share|improve this answer


























                                                        17












                                                        17








                                                        17







                                                        swift 4.0



                                                        let stringNumber = "123"
                                                        let number = Int(stringNumber) //here number is of type "Int?"


                                                        //using Forced Unwrapping

                                                        if number != nil {
                                                        //string is converted to Int
                                                        }


                                                        you could also use Optional Binding other than forced binding.



                                                        eg:



                                                          if let number = Int(stringNumber) { 
                                                        // number is of type Int
                                                        }





                                                        share|improve this answer













                                                        swift 4.0



                                                        let stringNumber = "123"
                                                        let number = Int(stringNumber) //here number is of type "Int?"


                                                        //using Forced Unwrapping

                                                        if number != nil {
                                                        //string is converted to Int
                                                        }


                                                        you could also use Optional Binding other than forced binding.



                                                        eg:



                                                          if let number = Int(stringNumber) { 
                                                        // number is of type Int
                                                        }






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Dec 12 '17 at 12:32









                                                        OOMMENOOMMEN

                                                        17926




                                                        17926























                                                            14














                                                            //Xcode 8.1 and swift 3.0



                                                            We can also handle it by Optional Binding, Simply



                                                            let occur = "10"

                                                            if let occ = Int(occur) {
                                                            print("By optional binding :", occ*2) // 20

                                                            }





                                                            share|improve this answer






























                                                              14














                                                              //Xcode 8.1 and swift 3.0



                                                              We can also handle it by Optional Binding, Simply



                                                              let occur = "10"

                                                              if let occ = Int(occur) {
                                                              print("By optional binding :", occ*2) // 20

                                                              }





                                                              share|improve this answer




























                                                                14












                                                                14








                                                                14







                                                                //Xcode 8.1 and swift 3.0



                                                                We can also handle it by Optional Binding, Simply



                                                                let occur = "10"

                                                                if let occ = Int(occur) {
                                                                print("By optional binding :", occ*2) // 20

                                                                }





                                                                share|improve this answer















                                                                //Xcode 8.1 and swift 3.0



                                                                We can also handle it by Optional Binding, Simply



                                                                let occur = "10"

                                                                if let occ = Int(occur) {
                                                                print("By optional binding :", occ*2) // 20

                                                                }






                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited May 25 '17 at 14:33









                                                                CupawnTae

                                                                11.3k22251




                                                                11.3k22251










                                                                answered Nov 25 '16 at 7:47









                                                                Pankaj NigamPankaj Nigam

                                                                17816




                                                                17816























                                                                    7














                                                                    Swift 3



                                                                    The simplest and more secure way is:



                                                                    @IBOutlet var textFieldA  : UITextField
                                                                    @IBOutlet var textFieldB : UITextField
                                                                    @IBOutlet var answerLabel : UILabel

                                                                    @IBAction func calculate(sender : AnyObject) {

                                                                    if let intValueA = Int(textFieldA),
                                                                    let intValueB = Int(textFieldB) {
                                                                    let result = intValueA + intValueB
                                                                    answerLabel.text = "The acceleration is (result)"
                                                                    }
                                                                    else {
                                                                    answerLabel.text = "The value (intValueA) and/or (intValueB) are not a valid integer value"
                                                                    }
                                                                    }


                                                                    Avoid invalid values setting keyboard type to number pad:



                                                                     textFieldA.keyboardType = .numberPad
                                                                    textFieldB.keyboardType = .numberPad





                                                                    share|improve this answer






























                                                                      7














                                                                      Swift 3



                                                                      The simplest and more secure way is:



                                                                      @IBOutlet var textFieldA  : UITextField
                                                                      @IBOutlet var textFieldB : UITextField
                                                                      @IBOutlet var answerLabel : UILabel

                                                                      @IBAction func calculate(sender : AnyObject) {

                                                                      if let intValueA = Int(textFieldA),
                                                                      let intValueB = Int(textFieldB) {
                                                                      let result = intValueA + intValueB
                                                                      answerLabel.text = "The acceleration is (result)"
                                                                      }
                                                                      else {
                                                                      answerLabel.text = "The value (intValueA) and/or (intValueB) are not a valid integer value"
                                                                      }
                                                                      }


                                                                      Avoid invalid values setting keyboard type to number pad:



                                                                       textFieldA.keyboardType = .numberPad
                                                                      textFieldB.keyboardType = .numberPad





                                                                      share|improve this answer




























                                                                        7












                                                                        7








                                                                        7







                                                                        Swift 3



                                                                        The simplest and more secure way is:



                                                                        @IBOutlet var textFieldA  : UITextField
                                                                        @IBOutlet var textFieldB : UITextField
                                                                        @IBOutlet var answerLabel : UILabel

                                                                        @IBAction func calculate(sender : AnyObject) {

                                                                        if let intValueA = Int(textFieldA),
                                                                        let intValueB = Int(textFieldB) {
                                                                        let result = intValueA + intValueB
                                                                        answerLabel.text = "The acceleration is (result)"
                                                                        }
                                                                        else {
                                                                        answerLabel.text = "The value (intValueA) and/or (intValueB) are not a valid integer value"
                                                                        }
                                                                        }


                                                                        Avoid invalid values setting keyboard type to number pad:



                                                                         textFieldA.keyboardType = .numberPad
                                                                        textFieldB.keyboardType = .numberPad





                                                                        share|improve this answer















                                                                        Swift 3



                                                                        The simplest and more secure way is:



                                                                        @IBOutlet var textFieldA  : UITextField
                                                                        @IBOutlet var textFieldB : UITextField
                                                                        @IBOutlet var answerLabel : UILabel

                                                                        @IBAction func calculate(sender : AnyObject) {

                                                                        if let intValueA = Int(textFieldA),
                                                                        let intValueB = Int(textFieldB) {
                                                                        let result = intValueA + intValueB
                                                                        answerLabel.text = "The acceleration is (result)"
                                                                        }
                                                                        else {
                                                                        answerLabel.text = "The value (intValueA) and/or (intValueB) are not a valid integer value"
                                                                        }
                                                                        }


                                                                        Avoid invalid values setting keyboard type to number pad:



                                                                         textFieldA.keyboardType = .numberPad
                                                                        textFieldB.keyboardType = .numberPad






                                                                        share|improve this answer














                                                                        share|improve this answer



                                                                        share|improve this answer








                                                                        edited Mar 14 '17 at 4:37









                                                                        John R Perry

                                                                        1,7781637




                                                                        1,7781637










                                                                        answered Oct 11 '16 at 18:03









                                                                        torcellytorcelly

                                                                        41147




                                                                        41147























                                                                            5














                                                                            In Swift 4:



                                                                            extension String {            
                                                                            var numberValue:NSNumber? {
                                                                            let formatter = NumberFormatter()
                                                                            formatter.numberStyle = .decimal
                                                                            return formatter.number(from: self)
                                                                            }
                                                                            }
                                                                            let someFloat = "12".numberValue





                                                                            share|improve this answer






























                                                                              5














                                                                              In Swift 4:



                                                                              extension String {            
                                                                              var numberValue:NSNumber? {
                                                                              let formatter = NumberFormatter()
                                                                              formatter.numberStyle = .decimal
                                                                              return formatter.number(from: self)
                                                                              }
                                                                              }
                                                                              let someFloat = "12".numberValue





                                                                              share|improve this answer




























                                                                                5












                                                                                5








                                                                                5







                                                                                In Swift 4:



                                                                                extension String {            
                                                                                var numberValue:NSNumber? {
                                                                                let formatter = NumberFormatter()
                                                                                formatter.numberStyle = .decimal
                                                                                return formatter.number(from: self)
                                                                                }
                                                                                }
                                                                                let someFloat = "12".numberValue





                                                                                share|improve this answer















                                                                                In Swift 4:



                                                                                extension String {            
                                                                                var numberValue:NSNumber? {
                                                                                let formatter = NumberFormatter()
                                                                                formatter.numberStyle = .decimal
                                                                                return formatter.number(from: self)
                                                                                }
                                                                                }
                                                                                let someFloat = "12".numberValue






                                                                                share|improve this answer














                                                                                share|improve this answer



                                                                                share|improve this answer








                                                                                edited Oct 30 '17 at 6:11

























                                                                                answered Oct 12 '17 at 18:53









                                                                                Ankit gargAnkit garg

                                                                                1,2111214




                                                                                1,2111214























                                                                                    4














                                                                                    i have made a simple program, where you have 2 txt field you take input form the user and add them to make it simpler to understand please find the code below.



                                                                                    @IBOutlet weak var result: UILabel!
                                                                                    @IBOutlet weak var one: UITextField!
                                                                                    @IBOutlet weak var two: UITextField!

                                                                                    @IBAction func add(sender: AnyObject) {
                                                                                    let count = Int(one.text!)
                                                                                    let cal = Int(two.text!)
                                                                                    let sum = count! + cal!
                                                                                    result.text = "Sum is (sum)"
                                                                                    }


                                                                                    hope this helps.






                                                                                    share|improve this answer






























                                                                                      4














                                                                                      i have made a simple program, where you have 2 txt field you take input form the user and add them to make it simpler to understand please find the code below.



                                                                                      @IBOutlet weak var result: UILabel!
                                                                                      @IBOutlet weak var one: UITextField!
                                                                                      @IBOutlet weak var two: UITextField!

                                                                                      @IBAction func add(sender: AnyObject) {
                                                                                      let count = Int(one.text!)
                                                                                      let cal = Int(two.text!)
                                                                                      let sum = count! + cal!
                                                                                      result.text = "Sum is (sum)"
                                                                                      }


                                                                                      hope this helps.






                                                                                      share|improve this answer




























                                                                                        4












                                                                                        4








                                                                                        4







                                                                                        i have made a simple program, where you have 2 txt field you take input form the user and add them to make it simpler to understand please find the code below.



                                                                                        @IBOutlet weak var result: UILabel!
                                                                                        @IBOutlet weak var one: UITextField!
                                                                                        @IBOutlet weak var two: UITextField!

                                                                                        @IBAction func add(sender: AnyObject) {
                                                                                        let count = Int(one.text!)
                                                                                        let cal = Int(two.text!)
                                                                                        let sum = count! + cal!
                                                                                        result.text = "Sum is (sum)"
                                                                                        }


                                                                                        hope this helps.






                                                                                        share|improve this answer















                                                                                        i have made a simple program, where you have 2 txt field you take input form the user and add them to make it simpler to understand please find the code below.



                                                                                        @IBOutlet weak var result: UILabel!
                                                                                        @IBOutlet weak var one: UITextField!
                                                                                        @IBOutlet weak var two: UITextField!

                                                                                        @IBAction func add(sender: AnyObject) {
                                                                                        let count = Int(one.text!)
                                                                                        let cal = Int(two.text!)
                                                                                        let sum = count! + cal!
                                                                                        result.text = "Sum is (sum)"
                                                                                        }


                                                                                        hope this helps.







                                                                                        share|improve this answer














                                                                                        share|improve this answer



                                                                                        share|improve this answer








                                                                                        edited Jan 11 '16 at 18:11









                                                                                        chedabob

                                                                                        5,34821940




                                                                                        5,34821940










                                                                                        answered Jan 11 '16 at 18:07









                                                                                        AliRaza QureshiAliRaza Qureshi

                                                                                        411




                                                                                        411























                                                                                            3














                                                                                            About int() and Swift 2.x: if you get a nil value after conversion check if you try to convert a string with a big number (for example: 1073741824), in this case try:



                                                                                            let bytesInternet : Int64 = Int64(bytesInternetString)!





                                                                                            share|improve this answer



















                                                                                            • 1





                                                                                              Thank you this worked for my case. Int() was working for me with 16 digit numbers but recently started to fail.

                                                                                              – Ryan Boyd
                                                                                              Apr 30 '16 at 7:03
















                                                                                            3














                                                                                            About int() and Swift 2.x: if you get a nil value after conversion check if you try to convert a string with a big number (for example: 1073741824), in this case try:



                                                                                            let bytesInternet : Int64 = Int64(bytesInternetString)!





                                                                                            share|improve this answer



















                                                                                            • 1





                                                                                              Thank you this worked for my case. Int() was working for me with 16 digit numbers but recently started to fail.

                                                                                              – Ryan Boyd
                                                                                              Apr 30 '16 at 7:03














                                                                                            3












                                                                                            3








                                                                                            3







                                                                                            About int() and Swift 2.x: if you get a nil value after conversion check if you try to convert a string with a big number (for example: 1073741824), in this case try:



                                                                                            let bytesInternet : Int64 = Int64(bytesInternetString)!





                                                                                            share|improve this answer













                                                                                            About int() and Swift 2.x: if you get a nil value after conversion check if you try to convert a string with a big number (for example: 1073741824), in this case try:



                                                                                            let bytesInternet : Int64 = Int64(bytesInternetString)!






                                                                                            share|improve this answer












                                                                                            share|improve this answer



                                                                                            share|improve this answer










                                                                                            answered Oct 30 '15 at 18:34









                                                                                            Alessandro OrnanoAlessandro Ornano

                                                                                            23.1k106585




                                                                                            23.1k106585








                                                                                            • 1





                                                                                              Thank you this worked for my case. Int() was working for me with 16 digit numbers but recently started to fail.

                                                                                              – Ryan Boyd
                                                                                              Apr 30 '16 at 7:03














                                                                                            • 1





                                                                                              Thank you this worked for my case. Int() was working for me with 16 digit numbers but recently started to fail.

                                                                                              – Ryan Boyd
                                                                                              Apr 30 '16 at 7:03








                                                                                            1




                                                                                            1





                                                                                            Thank you this worked for my case. Int() was working for me with 16 digit numbers but recently started to fail.

                                                                                            – Ryan Boyd
                                                                                            Apr 30 '16 at 7:03





                                                                                            Thank you this worked for my case. Int() was working for me with 16 digit numbers but recently started to fail.

                                                                                            – Ryan Boyd
                                                                                            Apr 30 '16 at 7:03











                                                                                            3














                                                                                            Latest swift3 this code is simply to convert string to int



                                                                                            let myString = "556"
                                                                                            let myInt = Int(myString)





                                                                                            share|improve this answer






























                                                                                              3














                                                                                              Latest swift3 this code is simply to convert string to int



                                                                                              let myString = "556"
                                                                                              let myInt = Int(myString)





                                                                                              share|improve this answer




























                                                                                                3












                                                                                                3








                                                                                                3







                                                                                                Latest swift3 this code is simply to convert string to int



                                                                                                let myString = "556"
                                                                                                let myInt = Int(myString)





                                                                                                share|improve this answer















                                                                                                Latest swift3 this code is simply to convert string to int



                                                                                                let myString = "556"
                                                                                                let myInt = Int(myString)






                                                                                                share|improve this answer














                                                                                                share|improve this answer



                                                                                                share|improve this answer








                                                                                                edited May 14 '17 at 5:40









                                                                                                shim

                                                                                                4,03564679




                                                                                                4,03564679










                                                                                                answered Mar 26 '17 at 17:12







                                                                                                user7642519






























                                                                                                    3














                                                                                                    Swift 3.0



                                                                                                    Try this, you don't need to check for any condition I have done everything just use this function. Send anything string, number, float, double ,etc,. you get a number as a value or 0 if it is unable to convert your value



                                                                                                    Function:



                                                                                                    func getNumber(number: Any?) -> NSNumber {
                                                                                                    guard let statusNumber:NSNumber = number as? NSNumber else
                                                                                                    {
                                                                                                    guard let statString:String = number as? String else
                                                                                                    {
                                                                                                    return 0
                                                                                                    }
                                                                                                    if let myInteger = Int(statString)
                                                                                                    {
                                                                                                    return NSNumber(value:myInteger)
                                                                                                    }
                                                                                                    else{
                                                                                                    return 0
                                                                                                    }
                                                                                                    }
                                                                                                    return statusNumber
                                                                                                    }


                                                                                                    Usage:
                                                                                                    Add the above function in code and to convert use
                                                                                                    let myNumber = getNumber(number: myString)
                                                                                                    if the myString has a number or string it returns the number else it returns 0



                                                                                                    Example 1:



                                                                                                    let number:String = "9834"
                                                                                                    print("printing number (getNumber(number: number))")


                                                                                                    Output: printing number 9834



                                                                                                    Example 2:



                                                                                                    let number:Double = 9834
                                                                                                    print("printing number (getNumber(number: number))")


                                                                                                    Output: printing number 9834



                                                                                                    Example 3:



                                                                                                    let number = 9834
                                                                                                    print("printing number (getNumber(number: number))")


                                                                                                    Output: printing number 9834






                                                                                                    share|improve this answer






























                                                                                                      3














                                                                                                      Swift 3.0



                                                                                                      Try this, you don't need to check for any condition I have done everything just use this function. Send anything string, number, float, double ,etc,. you get a number as a value or 0 if it is unable to convert your value



                                                                                                      Function:



                                                                                                      func getNumber(number: Any?) -> NSNumber {
                                                                                                      guard let statusNumber:NSNumber = number as? NSNumber else
                                                                                                      {
                                                                                                      guard let statString:String = number as? String else
                                                                                                      {
                                                                                                      return 0
                                                                                                      }
                                                                                                      if let myInteger = Int(statString)
                                                                                                      {
                                                                                                      return NSNumber(value:myInteger)
                                                                                                      }
                                                                                                      else{
                                                                                                      return 0
                                                                                                      }
                                                                                                      }
                                                                                                      return statusNumber
                                                                                                      }


                                                                                                      Usage:
                                                                                                      Add the above function in code and to convert use
                                                                                                      let myNumber = getNumber(number: myString)
                                                                                                      if the myString has a number or string it returns the number else it returns 0



                                                                                                      Example 1:



                                                                                                      let number:String = "9834"
                                                                                                      print("printing number (getNumber(number: number))")


                                                                                                      Output: printing number 9834



                                                                                                      Example 2:



                                                                                                      let number:Double = 9834
                                                                                                      print("printing number (getNumber(number: number))")


                                                                                                      Output: printing number 9834



                                                                                                      Example 3:



                                                                                                      let number = 9834
                                                                                                      print("printing number (getNumber(number: number))")


                                                                                                      Output: printing number 9834






                                                                                                      share|improve this answer




























                                                                                                        3












                                                                                                        3








                                                                                                        3







                                                                                                        Swift 3.0



                                                                                                        Try this, you don't need to check for any condition I have done everything just use this function. Send anything string, number, float, double ,etc,. you get a number as a value or 0 if it is unable to convert your value



                                                                                                        Function:



                                                                                                        func getNumber(number: Any?) -> NSNumber {
                                                                                                        guard let statusNumber:NSNumber = number as? NSNumber else
                                                                                                        {
                                                                                                        guard let statString:String = number as? String else
                                                                                                        {
                                                                                                        return 0
                                                                                                        }
                                                                                                        if let myInteger = Int(statString)
                                                                                                        {
                                                                                                        return NSNumber(value:myInteger)
                                                                                                        }
                                                                                                        else{
                                                                                                        return 0
                                                                                                        }
                                                                                                        }
                                                                                                        return statusNumber
                                                                                                        }


                                                                                                        Usage:
                                                                                                        Add the above function in code and to convert use
                                                                                                        let myNumber = getNumber(number: myString)
                                                                                                        if the myString has a number or string it returns the number else it returns 0



                                                                                                        Example 1:



                                                                                                        let number:String = "9834"
                                                                                                        print("printing number (getNumber(number: number))")


                                                                                                        Output: printing number 9834



                                                                                                        Example 2:



                                                                                                        let number:Double = 9834
                                                                                                        print("printing number (getNumber(number: number))")


                                                                                                        Output: printing number 9834



                                                                                                        Example 3:



                                                                                                        let number = 9834
                                                                                                        print("printing number (getNumber(number: number))")


                                                                                                        Output: printing number 9834






                                                                                                        share|improve this answer















                                                                                                        Swift 3.0



                                                                                                        Try this, you don't need to check for any condition I have done everything just use this function. Send anything string, number, float, double ,etc,. you get a number as a value or 0 if it is unable to convert your value



                                                                                                        Function:



                                                                                                        func getNumber(number: Any?) -> NSNumber {
                                                                                                        guard let statusNumber:NSNumber = number as? NSNumber else
                                                                                                        {
                                                                                                        guard let statString:String = number as? String else
                                                                                                        {
                                                                                                        return 0
                                                                                                        }
                                                                                                        if let myInteger = Int(statString)
                                                                                                        {
                                                                                                        return NSNumber(value:myInteger)
                                                                                                        }
                                                                                                        else{
                                                                                                        return 0
                                                                                                        }
                                                                                                        }
                                                                                                        return statusNumber
                                                                                                        }


                                                                                                        Usage:
                                                                                                        Add the above function in code and to convert use
                                                                                                        let myNumber = getNumber(number: myString)
                                                                                                        if the myString has a number or string it returns the number else it returns 0



                                                                                                        Example 1:



                                                                                                        let number:String = "9834"
                                                                                                        print("printing number (getNumber(number: number))")


                                                                                                        Output: printing number 9834



                                                                                                        Example 2:



                                                                                                        let number:Double = 9834
                                                                                                        print("printing number (getNumber(number: number))")


                                                                                                        Output: printing number 9834



                                                                                                        Example 3:



                                                                                                        let number = 9834
                                                                                                        print("printing number (getNumber(number: number))")


                                                                                                        Output: printing number 9834







                                                                                                        share|improve this answer














                                                                                                        share|improve this answer



                                                                                                        share|improve this answer








                                                                                                        edited Nov 7 '17 at 6:34

























                                                                                                        answered Jan 24 '17 at 6:12









                                                                                                        KoushikKoushik

                                                                                                        7601117




                                                                                                        7601117























                                                                                                            3














                                                                                                            In Swift 4.2 and Xcode 10.1



                                                                                                            let string:String = "789"
                                                                                                            let intValue:Int = Int(string)!
                                                                                                            print(intValue)

                                                                                                            let integerValue:Int = 789
                                                                                                            let stringValue:String = String(integerValue)
                                                                                                            //OR
                                                                                                            //let stringValue:String = "(integerValue)"
                                                                                                            print(stringValue)





                                                                                                            share|improve this answer






























                                                                                                              3














                                                                                                              In Swift 4.2 and Xcode 10.1



                                                                                                              let string:String = "789"
                                                                                                              let intValue:Int = Int(string)!
                                                                                                              print(intValue)

                                                                                                              let integerValue:Int = 789
                                                                                                              let stringValue:String = String(integerValue)
                                                                                                              //OR
                                                                                                              //let stringValue:String = "(integerValue)"
                                                                                                              print(stringValue)





                                                                                                              share|improve this answer




























                                                                                                                3












                                                                                                                3








                                                                                                                3







                                                                                                                In Swift 4.2 and Xcode 10.1



                                                                                                                let string:String = "789"
                                                                                                                let intValue:Int = Int(string)!
                                                                                                                print(intValue)

                                                                                                                let integerValue:Int = 789
                                                                                                                let stringValue:String = String(integerValue)
                                                                                                                //OR
                                                                                                                //let stringValue:String = "(integerValue)"
                                                                                                                print(stringValue)





                                                                                                                share|improve this answer















                                                                                                                In Swift 4.2 and Xcode 10.1



                                                                                                                let string:String = "789"
                                                                                                                let intValue:Int = Int(string)!
                                                                                                                print(intValue)

                                                                                                                let integerValue:Int = 789
                                                                                                                let stringValue:String = String(integerValue)
                                                                                                                //OR
                                                                                                                //let stringValue:String = "(integerValue)"
                                                                                                                print(stringValue)






                                                                                                                share|improve this answer














                                                                                                                share|improve this answer



                                                                                                                share|improve this answer








                                                                                                                edited Jan 1 at 5:04

























                                                                                                                answered Oct 12 '18 at 6:50









                                                                                                                iOSiOS

                                                                                                                2,76722047




                                                                                                                2,76722047























                                                                                                                    2














                                                                                                                    Because a string might contain non-numerical characters you should use a guard to protect the operation. Example:



                                                                                                                    guard let labelInt:Int = Int(labelString) else {
                                                                                                                    return
                                                                                                                    }

                                                                                                                    useLabelInt()





                                                                                                                    share|improve this answer




























                                                                                                                      2














                                                                                                                      Because a string might contain non-numerical characters you should use a guard to protect the operation. Example:



                                                                                                                      guard let labelInt:Int = Int(labelString) else {
                                                                                                                      return
                                                                                                                      }

                                                                                                                      useLabelInt()





                                                                                                                      share|improve this answer


























                                                                                                                        2












                                                                                                                        2








                                                                                                                        2







                                                                                                                        Because a string might contain non-numerical characters you should use a guard to protect the operation. Example:



                                                                                                                        guard let labelInt:Int = Int(labelString) else {
                                                                                                                        return
                                                                                                                        }

                                                                                                                        useLabelInt()





                                                                                                                        share|improve this answer













                                                                                                                        Because a string might contain non-numerical characters you should use a guard to protect the operation. Example:



                                                                                                                        guard let labelInt:Int = Int(labelString) else {
                                                                                                                        return
                                                                                                                        }

                                                                                                                        useLabelInt()






                                                                                                                        share|improve this answer












                                                                                                                        share|improve this answer



                                                                                                                        share|improve this answer










                                                                                                                        answered Feb 22 '18 at 8:38









                                                                                                                        RonTLVRonTLV

                                                                                                                        1,2262923




                                                                                                                        1,2262923























                                                                                                                            1














                                                                                                                            Use this:



                                                                                                                            // get the values from text boxes
                                                                                                                            let a:Double = firstText.text.bridgeToObjectiveC().doubleValue
                                                                                                                            let b:Double = secondText.text.bridgeToObjectiveC().doubleValue

                                                                                                                            // we checking against 0.0, because above function return 0.0 if it gets failed to convert
                                                                                                                            if (a != 0.0) && (b != 0.0) {
                                                                                                                            var ans = a + b
                                                                                                                            answerLabel.text = "Answer is (ans)"
                                                                                                                            } else {
                                                                                                                            answerLabel.text = "Input values are not numberic"
                                                                                                                            }


                                                                                                                            OR



                                                                                                                            Make your UITextField KeyboardType as DecimalTab from your XIB or storyboard, and remove any if condition for doing any calculation, ie.



                                                                                                                            var ans = a + b
                                                                                                                            answerLabel.text = "Answer is (ans)"


                                                                                                                            Because keyboard type is DecimalPad there is no chance to enter other 0-9 or .



                                                                                                                            Hope this help !!






                                                                                                                            share|improve this answer






























                                                                                                                              1














                                                                                                                              Use this:



                                                                                                                              // get the values from text boxes
                                                                                                                              let a:Double = firstText.text.bridgeToObjectiveC().doubleValue
                                                                                                                              let b:Double = secondText.text.bridgeToObjectiveC().doubleValue

                                                                                                                              // we checking against 0.0, because above function return 0.0 if it gets failed to convert
                                                                                                                              if (a != 0.0) && (b != 0.0) {
                                                                                                                              var ans = a + b
                                                                                                                              answerLabel.text = "Answer is (ans)"
                                                                                                                              } else {
                                                                                                                              answerLabel.text = "Input values are not numberic"
                                                                                                                              }


                                                                                                                              OR



                                                                                                                              Make your UITextField KeyboardType as DecimalTab from your XIB or storyboard, and remove any if condition for doing any calculation, ie.



                                                                                                                              var ans = a + b
                                                                                                                              answerLabel.text = "Answer is (ans)"


                                                                                                                              Because keyboard type is DecimalPad there is no chance to enter other 0-9 or .



                                                                                                                              Hope this help !!






                                                                                                                              share|improve this answer




























                                                                                                                                1












                                                                                                                                1








                                                                                                                                1







                                                                                                                                Use this:



                                                                                                                                // get the values from text boxes
                                                                                                                                let a:Double = firstText.text.bridgeToObjectiveC().doubleValue
                                                                                                                                let b:Double = secondText.text.bridgeToObjectiveC().doubleValue

                                                                                                                                // we checking against 0.0, because above function return 0.0 if it gets failed to convert
                                                                                                                                if (a != 0.0) && (b != 0.0) {
                                                                                                                                var ans = a + b
                                                                                                                                answerLabel.text = "Answer is (ans)"
                                                                                                                                } else {
                                                                                                                                answerLabel.text = "Input values are not numberic"
                                                                                                                                }


                                                                                                                                OR



                                                                                                                                Make your UITextField KeyboardType as DecimalTab from your XIB or storyboard, and remove any if condition for doing any calculation, ie.



                                                                                                                                var ans = a + b
                                                                                                                                answerLabel.text = "Answer is (ans)"


                                                                                                                                Because keyboard type is DecimalPad there is no chance to enter other 0-9 or .



                                                                                                                                Hope this help !!






                                                                                                                                share|improve this answer















                                                                                                                                Use this:



                                                                                                                                // get the values from text boxes
                                                                                                                                let a:Double = firstText.text.bridgeToObjectiveC().doubleValue
                                                                                                                                let b:Double = secondText.text.bridgeToObjectiveC().doubleValue

                                                                                                                                // we checking against 0.0, because above function return 0.0 if it gets failed to convert
                                                                                                                                if (a != 0.0) && (b != 0.0) {
                                                                                                                                var ans = a + b
                                                                                                                                answerLabel.text = "Answer is (ans)"
                                                                                                                                } else {
                                                                                                                                answerLabel.text = "Input values are not numberic"
                                                                                                                                }


                                                                                                                                OR



                                                                                                                                Make your UITextField KeyboardType as DecimalTab from your XIB or storyboard, and remove any if condition for doing any calculation, ie.



                                                                                                                                var ans = a + b
                                                                                                                                answerLabel.text = "Answer is (ans)"


                                                                                                                                Because keyboard type is DecimalPad there is no chance to enter other 0-9 or .



                                                                                                                                Hope this help !!







                                                                                                                                share|improve this answer














                                                                                                                                share|improve this answer



                                                                                                                                share|improve this answer








                                                                                                                                edited Jun 13 '14 at 10:03

























                                                                                                                                answered Jun 13 '14 at 6:51









                                                                                                                                Narendar Singh SainiNarendar Singh Saini

                                                                                                                                2,4101814




                                                                                                                                2,4101814























                                                                                                                                    1














                                                                                                                                    //  To convert user input (i.e string) to int for calculation.I did this , and it works.


                                                                                                                                    let num:Int? = Int(firstTextField.text!);

                                                                                                                                    let sum:Int = num!-2

                                                                                                                                    print(sum);





                                                                                                                                    share|improve this answer






























                                                                                                                                      1














                                                                                                                                      //  To convert user input (i.e string) to int for calculation.I did this , and it works.


                                                                                                                                      let num:Int? = Int(firstTextField.text!);

                                                                                                                                      let sum:Int = num!-2

                                                                                                                                      print(sum);





                                                                                                                                      share|improve this answer




























                                                                                                                                        1












                                                                                                                                        1








                                                                                                                                        1







                                                                                                                                        //  To convert user input (i.e string) to int for calculation.I did this , and it works.


                                                                                                                                        let num:Int? = Int(firstTextField.text!);

                                                                                                                                        let sum:Int = num!-2

                                                                                                                                        print(sum);





                                                                                                                                        share|improve this answer















                                                                                                                                        //  To convert user input (i.e string) to int for calculation.I did this , and it works.


                                                                                                                                        let num:Int? = Int(firstTextField.text!);

                                                                                                                                        let sum:Int = num!-2

                                                                                                                                        print(sum);






                                                                                                                                        share|improve this answer














                                                                                                                                        share|improve this answer



                                                                                                                                        share|improve this answer








                                                                                                                                        edited Nov 21 '15 at 10:43

























                                                                                                                                        answered Nov 20 '15 at 2:55









                                                                                                                                        JennyJenny

                                                                                                                                        277




                                                                                                                                        277























                                                                                                                                            1














                                                                                                                                            This works for me



                                                                                                                                            var a:Int? = Int(userInput.text!)





                                                                                                                                            share|improve this answer


























                                                                                                                                            • How is this different from the solution given in the comment?

                                                                                                                                              – Prune
                                                                                                                                              Oct 15 '15 at 22:25






                                                                                                                                            • 2





                                                                                                                                              The solution given in the comment is missing "!" at the end , which is expected in Swift 2 and onwards

                                                                                                                                              – Naishta
                                                                                                                                              Oct 22 '15 at 20:45
















                                                                                                                                            1














                                                                                                                                            This works for me



                                                                                                                                            var a:Int? = Int(userInput.text!)





                                                                                                                                            share|improve this answer


























                                                                                                                                            • How is this different from the solution given in the comment?

                                                                                                                                              – Prune
                                                                                                                                              Oct 15 '15 at 22:25






                                                                                                                                            • 2





                                                                                                                                              The solution given in the comment is missing "!" at the end , which is expected in Swift 2 and onwards

                                                                                                                                              – Naishta
                                                                                                                                              Oct 22 '15 at 20:45














                                                                                                                                            1












                                                                                                                                            1








                                                                                                                                            1







                                                                                                                                            This works for me



                                                                                                                                            var a:Int? = Int(userInput.text!)





                                                                                                                                            share|improve this answer















                                                                                                                                            This works for me



                                                                                                                                            var a:Int? = Int(userInput.text!)






                                                                                                                                            share|improve this answer














                                                                                                                                            share|improve this answer



                                                                                                                                            share|improve this answer








                                                                                                                                            edited Dec 2 '16 at 5:14









                                                                                                                                            rptwsthi

                                                                                                                                            8,56885393




                                                                                                                                            8,56885393










                                                                                                                                            answered Oct 15 '15 at 21:52









                                                                                                                                            NaishtaNaishta

                                                                                                                                            6,17934039




                                                                                                                                            6,17934039













                                                                                                                                            • How is this different from the solution given in the comment?

                                                                                                                                              – Prune
                                                                                                                                              Oct 15 '15 at 22:25






                                                                                                                                            • 2





                                                                                                                                              The solution given in the comment is missing "!" at the end , which is expected in Swift 2 and onwards

                                                                                                                                              – Naishta
                                                                                                                                              Oct 22 '15 at 20:45



















                                                                                                                                            • How is this different from the solution given in the comment?

                                                                                                                                              – Prune
                                                                                                                                              Oct 15 '15 at 22:25






                                                                                                                                            • 2





                                                                                                                                              The solution given in the comment is missing "!" at the end , which is expected in Swift 2 and onwards

                                                                                                                                              – Naishta
                                                                                                                                              Oct 22 '15 at 20:45

















                                                                                                                                            How is this different from the solution given in the comment?

                                                                                                                                            – Prune
                                                                                                                                            Oct 15 '15 at 22:25





                                                                                                                                            How is this different from the solution given in the comment?

                                                                                                                                            – Prune
                                                                                                                                            Oct 15 '15 at 22:25




                                                                                                                                            2




                                                                                                                                            2





                                                                                                                                            The solution given in the comment is missing "!" at the end , which is expected in Swift 2 and onwards

                                                                                                                                            – Naishta
                                                                                                                                            Oct 22 '15 at 20:45





                                                                                                                                            The solution given in the comment is missing "!" at the end , which is expected in Swift 2 and onwards

                                                                                                                                            – Naishta
                                                                                                                                            Oct 22 '15 at 20:45











                                                                                                                                            1














                                                                                                                                            for Swift3.x



                                                                                                                                            extension String {
                                                                                                                                            func toInt(defaultValue: Int) -> Int {
                                                                                                                                            if let n = Int(self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)) {
                                                                                                                                            return n
                                                                                                                                            } else {
                                                                                                                                            return defaultValue
                                                                                                                                            }
                                                                                                                                            }
                                                                                                                                            }





                                                                                                                                            share|improve this answer




























                                                                                                                                              1














                                                                                                                                              for Swift3.x



                                                                                                                                              extension String {
                                                                                                                                              func toInt(defaultValue: Int) -> Int {
                                                                                                                                              if let n = Int(self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)) {
                                                                                                                                              return n
                                                                                                                                              } else {
                                                                                                                                              return defaultValue
                                                                                                                                              }
                                                                                                                                              }
                                                                                                                                              }





                                                                                                                                              share|improve this answer


























                                                                                                                                                1












                                                                                                                                                1








                                                                                                                                                1







                                                                                                                                                for Swift3.x



                                                                                                                                                extension String {
                                                                                                                                                func toInt(defaultValue: Int) -> Int {
                                                                                                                                                if let n = Int(self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)) {
                                                                                                                                                return n
                                                                                                                                                } else {
                                                                                                                                                return defaultValue
                                                                                                                                                }
                                                                                                                                                }
                                                                                                                                                }





                                                                                                                                                share|improve this answer













                                                                                                                                                for Swift3.x



                                                                                                                                                extension String {
                                                                                                                                                func toInt(defaultValue: Int) -> Int {
                                                                                                                                                if let n = Int(self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)) {
                                                                                                                                                return n
                                                                                                                                                } else {
                                                                                                                                                return defaultValue
                                                                                                                                                }
                                                                                                                                                }
                                                                                                                                                }






                                                                                                                                                share|improve this answer












                                                                                                                                                share|improve this answer



                                                                                                                                                share|improve this answer










                                                                                                                                                answered Apr 28 '17 at 13:02









                                                                                                                                                User9527User9527

                                                                                                                                                2,7852129




                                                                                                                                                2,7852129























                                                                                                                                                    1














                                                                                                                                                    In Swift 2.x, the .toInt() function was removed from String. In replacement, Int now has an initializer that accepts a String



                                                                                                                                                    Int(myString)



                                                                                                                                                    In your case, you could use Int(textField.text!) insted of textField.text!.toInt()



                                                                                                                                                    Swift 1.x



                                                                                                                                                    let myString: String = "256"
                                                                                                                                                    let myInt: Int? = myString.toInt()


                                                                                                                                                    Swift 2.x, 3.x



                                                                                                                                                    let myString: String = "256"
                                                                                                                                                    let myInt: Int? = Int(myString)





                                                                                                                                                    share|improve this answer




























                                                                                                                                                      1














                                                                                                                                                      In Swift 2.x, the .toInt() function was removed from String. In replacement, Int now has an initializer that accepts a String



                                                                                                                                                      Int(myString)



                                                                                                                                                      In your case, you could use Int(textField.text!) insted of textField.text!.toInt()



                                                                                                                                                      Swift 1.x



                                                                                                                                                      let myString: String = "256"
                                                                                                                                                      let myInt: Int? = myString.toInt()


                                                                                                                                                      Swift 2.x, 3.x



                                                                                                                                                      let myString: String = "256"
                                                                                                                                                      let myInt: Int? = Int(myString)





                                                                                                                                                      share|improve this answer


























                                                                                                                                                        1












                                                                                                                                                        1








                                                                                                                                                        1







                                                                                                                                                        In Swift 2.x, the .toInt() function was removed from String. In replacement, Int now has an initializer that accepts a String



                                                                                                                                                        Int(myString)



                                                                                                                                                        In your case, you could use Int(textField.text!) insted of textField.text!.toInt()



                                                                                                                                                        Swift 1.x



                                                                                                                                                        let myString: String = "256"
                                                                                                                                                        let myInt: Int? = myString.toInt()


                                                                                                                                                        Swift 2.x, 3.x



                                                                                                                                                        let myString: String = "256"
                                                                                                                                                        let myInt: Int? = Int(myString)





                                                                                                                                                        share|improve this answer













                                                                                                                                                        In Swift 2.x, the .toInt() function was removed from String. In replacement, Int now has an initializer that accepts a String



                                                                                                                                                        Int(myString)



                                                                                                                                                        In your case, you could use Int(textField.text!) insted of textField.text!.toInt()



                                                                                                                                                        Swift 1.x



                                                                                                                                                        let myString: String = "256"
                                                                                                                                                        let myInt: Int? = myString.toInt()


                                                                                                                                                        Swift 2.x, 3.x



                                                                                                                                                        let myString: String = "256"
                                                                                                                                                        let myInt: Int? = Int(myString)






                                                                                                                                                        share|improve this answer












                                                                                                                                                        share|improve this answer



                                                                                                                                                        share|improve this answer










                                                                                                                                                        answered Mar 16 '18 at 7:32









                                                                                                                                                        AzarmaneshAzarmanesh

                                                                                                                                                        235




                                                                                                                                                        235























                                                                                                                                                            0














                                                                                                                                                            for Alternative solution. You can use extension a native type. You can test with playground.



                                                                                                                                                            extension String {
                                                                                                                                                            func add(a: Int) -> Int? {
                                                                                                                                                            if let b = Int(self) {
                                                                                                                                                            return b + a
                                                                                                                                                            }
                                                                                                                                                            else {
                                                                                                                                                            return nil
                                                                                                                                                            }
                                                                                                                                                            }
                                                                                                                                                            }


                                                                                                                                                            "2".add(1)






                                                                                                                                                            share|improve this answer




























                                                                                                                                                              0














                                                                                                                                                              for Alternative solution. You can use extension a native type. You can test with playground.



                                                                                                                                                              extension String {
                                                                                                                                                              func add(a: Int) -> Int? {
                                                                                                                                                              if let b = Int(self) {
                                                                                                                                                              return b + a
                                                                                                                                                              }
                                                                                                                                                              else {
                                                                                                                                                              return nil
                                                                                                                                                              }
                                                                                                                                                              }
                                                                                                                                                              }


                                                                                                                                                              "2".add(1)






                                                                                                                                                              share|improve this answer


























                                                                                                                                                                0












                                                                                                                                                                0








                                                                                                                                                                0







                                                                                                                                                                for Alternative solution. You can use extension a native type. You can test with playground.



                                                                                                                                                                extension String {
                                                                                                                                                                func add(a: Int) -> Int? {
                                                                                                                                                                if let b = Int(self) {
                                                                                                                                                                return b + a
                                                                                                                                                                }
                                                                                                                                                                else {
                                                                                                                                                                return nil
                                                                                                                                                                }
                                                                                                                                                                }
                                                                                                                                                                }


                                                                                                                                                                "2".add(1)






                                                                                                                                                                share|improve this answer













                                                                                                                                                                for Alternative solution. You can use extension a native type. You can test with playground.



                                                                                                                                                                extension String {
                                                                                                                                                                func add(a: Int) -> Int? {
                                                                                                                                                                if let b = Int(self) {
                                                                                                                                                                return b + a
                                                                                                                                                                }
                                                                                                                                                                else {
                                                                                                                                                                return nil
                                                                                                                                                                }
                                                                                                                                                                }
                                                                                                                                                                }


                                                                                                                                                                "2".add(1)







                                                                                                                                                                share|improve this answer












                                                                                                                                                                share|improve this answer



                                                                                                                                                                share|improve this answer










                                                                                                                                                                answered Apr 9 '16 at 16:12









                                                                                                                                                                Durul DalkanatDurul Dalkanat

                                                                                                                                                                4,95632732




                                                                                                                                                                4,95632732























                                                                                                                                                                    0














                                                                                                                                                                    My solution is to have a general extension for string to int conversion.



                                                                                                                                                                    extension String {

                                                                                                                                                                    // default: it is a number suitable for your project if the string is not an integer

                                                                                                                                                                    func toInt(default: Int) -> Int {
                                                                                                                                                                    if let result = Int(self) {
                                                                                                                                                                    return result
                                                                                                                                                                    }
                                                                                                                                                                    else {
                                                                                                                                                                    return default
                                                                                                                                                                    }
                                                                                                                                                                    }

                                                                                                                                                                    }





                                                                                                                                                                    share|improve this answer






























                                                                                                                                                                      0














                                                                                                                                                                      My solution is to have a general extension for string to int conversion.



                                                                                                                                                                      extension String {

                                                                                                                                                                      // default: it is a number suitable for your project if the string is not an integer

                                                                                                                                                                      func toInt(default: Int) -> Int {
                                                                                                                                                                      if let result = Int(self) {
                                                                                                                                                                      return result
                                                                                                                                                                      }
                                                                                                                                                                      else {
                                                                                                                                                                      return default
                                                                                                                                                                      }
                                                                                                                                                                      }

                                                                                                                                                                      }





                                                                                                                                                                      share|improve this answer




























                                                                                                                                                                        0












                                                                                                                                                                        0








                                                                                                                                                                        0







                                                                                                                                                                        My solution is to have a general extension for string to int conversion.



                                                                                                                                                                        extension String {

                                                                                                                                                                        // default: it is a number suitable for your project if the string is not an integer

                                                                                                                                                                        func toInt(default: Int) -> Int {
                                                                                                                                                                        if let result = Int(self) {
                                                                                                                                                                        return result
                                                                                                                                                                        }
                                                                                                                                                                        else {
                                                                                                                                                                        return default
                                                                                                                                                                        }
                                                                                                                                                                        }

                                                                                                                                                                        }





                                                                                                                                                                        share|improve this answer















                                                                                                                                                                        My solution is to have a general extension for string to int conversion.



                                                                                                                                                                        extension String {

                                                                                                                                                                        // default: it is a number suitable for your project if the string is not an integer

                                                                                                                                                                        func toInt(default: Int) -> Int {
                                                                                                                                                                        if let result = Int(self) {
                                                                                                                                                                        return result
                                                                                                                                                                        }
                                                                                                                                                                        else {
                                                                                                                                                                        return default
                                                                                                                                                                        }
                                                                                                                                                                        }

                                                                                                                                                                        }






                                                                                                                                                                        share|improve this answer














                                                                                                                                                                        share|improve this answer



                                                                                                                                                                        share|improve this answer








                                                                                                                                                                        edited Jan 4 '17 at 11:44

























                                                                                                                                                                        answered Jan 4 '17 at 8:28









                                                                                                                                                                        flame3flame3

                                                                                                                                                                        1,4581221




                                                                                                                                                                        1,4581221























                                                                                                                                                                            0














                                                                                                                                                                            @IBAction func calculateAclr(_ sender: Any) {
                                                                                                                                                                            if let addition = addition(arrayString: [txtBox1.text, txtBox2.text, txtBox3.text]) {
                                                                                                                                                                            print("Answer = (addition)")
                                                                                                                                                                            lblAnswer.text = "(addition)"
                                                                                                                                                                            }
                                                                                                                                                                            }

                                                                                                                                                                            func addition(arrayString: [Any?]) -> Int? {

                                                                                                                                                                            var answer:Int?
                                                                                                                                                                            for arrayElement in arrayString {
                                                                                                                                                                            if let stringValue = arrayElement, let intValue = Int(stringValue) {
                                                                                                                                                                            answer = (answer ?? 0) + intValue
                                                                                                                                                                            }
                                                                                                                                                                            }

                                                                                                                                                                            return answer
                                                                                                                                                                            }





                                                                                                                                                                            share|improve this answer






























                                                                                                                                                                              0














                                                                                                                                                                              @IBAction func calculateAclr(_ sender: Any) {
                                                                                                                                                                              if let addition = addition(arrayString: [txtBox1.text, txtBox2.text, txtBox3.text]) {
                                                                                                                                                                              print("Answer = (addition)")
                                                                                                                                                                              lblAnswer.text = "(addition)"
                                                                                                                                                                              }
                                                                                                                                                                              }

                                                                                                                                                                              func addition(arrayString: [Any?]) -> Int? {

                                                                                                                                                                              var answer:Int?
                                                                                                                                                                              for arrayElement in arrayString {
                                                                                                                                                                              if let stringValue = arrayElement, let intValue = Int(stringValue) {
                                                                                                                                                                              answer = (answer ?? 0) + intValue
                                                                                                                                                                              }
                                                                                                                                                                              }

                                                                                                                                                                              return answer
                                                                                                                                                                              }





                                                                                                                                                                              share|improve this answer




























                                                                                                                                                                                0












                                                                                                                                                                                0








                                                                                                                                                                                0







                                                                                                                                                                                @IBAction func calculateAclr(_ sender: Any) {
                                                                                                                                                                                if let addition = addition(arrayString: [txtBox1.text, txtBox2.text, txtBox3.text]) {
                                                                                                                                                                                print("Answer = (addition)")
                                                                                                                                                                                lblAnswer.text = "(addition)"
                                                                                                                                                                                }
                                                                                                                                                                                }

                                                                                                                                                                                func addition(arrayString: [Any?]) -> Int? {

                                                                                                                                                                                var answer:Int?
                                                                                                                                                                                for arrayElement in arrayString {
                                                                                                                                                                                if let stringValue = arrayElement, let intValue = Int(stringValue) {
                                                                                                                                                                                answer = (answer ?? 0) + intValue
                                                                                                                                                                                }
                                                                                                                                                                                }

                                                                                                                                                                                return answer
                                                                                                                                                                                }





                                                                                                                                                                                share|improve this answer















                                                                                                                                                                                @IBAction func calculateAclr(_ sender: Any) {
                                                                                                                                                                                if let addition = addition(arrayString: [txtBox1.text, txtBox2.text, txtBox3.text]) {
                                                                                                                                                                                print("Answer = (addition)")
                                                                                                                                                                                lblAnswer.text = "(addition)"
                                                                                                                                                                                }
                                                                                                                                                                                }

                                                                                                                                                                                func addition(arrayString: [Any?]) -> Int? {

                                                                                                                                                                                var answer:Int?
                                                                                                                                                                                for arrayElement in arrayString {
                                                                                                                                                                                if let stringValue = arrayElement, let intValue = Int(stringValue) {
                                                                                                                                                                                answer = (answer ?? 0) + intValue
                                                                                                                                                                                }
                                                                                                                                                                                }

                                                                                                                                                                                return answer
                                                                                                                                                                                }






                                                                                                                                                                                share|improve this answer














                                                                                                                                                                                share|improve this answer



                                                                                                                                                                                share|improve this answer








                                                                                                                                                                                edited Jul 24 '17 at 9:59

























                                                                                                                                                                                answered Jul 23 '17 at 6:46









                                                                                                                                                                                KrunalKrunal

                                                                                                                                                                                40.3k20151171




                                                                                                                                                                                40.3k20151171























                                                                                                                                                                                    0














                                                                                                                                                                                    As of swift 3, I have to force my #%@! string & int with a "!" otherwise it just doesn't work.



                                                                                                                                                                                    For example:



                                                                                                                                                                                    let prefs = UserDefaults.standard
                                                                                                                                                                                    var counter: String!
                                                                                                                                                                                    counter = prefs.string(forKey:"counter")
                                                                                                                                                                                    print("counter: (counter!)")


                                                                                                                                                                                    var counterInt = Int(counter!)
                                                                                                                                                                                    counterInt = counterInt! + 1
                                                                                                                                                                                    print("counterInt: (counterInt!)")

                                                                                                                                                                                    OUTPUT:
                                                                                                                                                                                    counter: 1
                                                                                                                                                                                    counterInt: 2





                                                                                                                                                                                    share|improve this answer
























                                                                                                                                                                                    • Can't you just do var counterInt = counter.map { Int($0) } ? Where counter whould be a String?

                                                                                                                                                                                      – Martin
                                                                                                                                                                                      Oct 19 '17 at 16:34











                                                                                                                                                                                    • @Martin - No. ? makes its optional and thus adds the word "optional" to the counter string.

                                                                                                                                                                                      – Sam B
                                                                                                                                                                                      Oct 19 '17 at 22:27











                                                                                                                                                                                    • IMHO, you should not force unwrap your optionals. Prefer use guard and if let statements

                                                                                                                                                                                      – Martin
                                                                                                                                                                                      Oct 20 '17 at 7:56
















                                                                                                                                                                                    0














                                                                                                                                                                                    As of swift 3, I have to force my #%@! string & int with a "!" otherwise it just doesn't work.



                                                                                                                                                                                    For example:



                                                                                                                                                                                    let prefs = UserDefaults.standard
                                                                                                                                                                                    var counter: String!
                                                                                                                                                                                    counter = prefs.string(forKey:"counter")
                                                                                                                                                                                    print("counter: (counter!)")


                                                                                                                                                                                    var counterInt = Int(counter!)
                                                                                                                                                                                    counterInt = counterInt! + 1
                                                                                                                                                                                    print("counterInt: (counterInt!)")

                                                                                                                                                                                    OUTPUT:
                                                                                                                                                                                    counter: 1
                                                                                                                                                                                    counterInt: 2





                                                                                                                                                                                    share|improve this answer
























                                                                                                                                                                                    • Can't you just do var counterInt = counter.map { Int($0) } ? Where counter whould be a String?

                                                                                                                                                                                      – Martin
                                                                                                                                                                                      Oct 19 '17 at 16:34











                                                                                                                                                                                    • @Martin - No. ? makes its optional and thus adds the word "optional" to the counter string.

                                                                                                                                                                                      – Sam B
                                                                                                                                                                                      Oct 19 '17 at 22:27











                                                                                                                                                                                    • IMHO, you should not force unwrap your optionals. Prefer use guard and if let statements

                                                                                                                                                                                      – Martin
                                                                                                                                                                                      Oct 20 '17 at 7:56














                                                                                                                                                                                    0












                                                                                                                                                                                    0








                                                                                                                                                                                    0







                                                                                                                                                                                    As of swift 3, I have to force my #%@! string & int with a "!" otherwise it just doesn't work.



                                                                                                                                                                                    For example:



                                                                                                                                                                                    let prefs = UserDefaults.standard
                                                                                                                                                                                    var counter: String!
                                                                                                                                                                                    counter = prefs.string(forKey:"counter")
                                                                                                                                                                                    print("counter: (counter!)")


                                                                                                                                                                                    var counterInt = Int(counter!)
                                                                                                                                                                                    counterInt = counterInt! + 1
                                                                                                                                                                                    print("counterInt: (counterInt!)")

                                                                                                                                                                                    OUTPUT:
                                                                                                                                                                                    counter: 1
                                                                                                                                                                                    counterInt: 2





                                                                                                                                                                                    share|improve this answer













                                                                                                                                                                                    As of swift 3, I have to force my #%@! string & int with a "!" otherwise it just doesn't work.



                                                                                                                                                                                    For example:



                                                                                                                                                                                    let prefs = UserDefaults.standard
                                                                                                                                                                                    var counter: String!
                                                                                                                                                                                    counter = prefs.string(forKey:"counter")
                                                                                                                                                                                    print("counter: (counter!)")


                                                                                                                                                                                    var counterInt = Int(counter!)
                                                                                                                                                                                    counterInt = counterInt! + 1
                                                                                                                                                                                    print("counterInt: (counterInt!)")

                                                                                                                                                                                    OUTPUT:
                                                                                                                                                                                    counter: 1
                                                                                                                                                                                    counterInt: 2






                                                                                                                                                                                    share|improve this answer












                                                                                                                                                                                    share|improve this answer



                                                                                                                                                                                    share|improve this answer










                                                                                                                                                                                    answered Oct 16 '17 at 21:41









                                                                                                                                                                                    Sam BSam B

                                                                                                                                                                                    19.7k1172110




                                                                                                                                                                                    19.7k1172110













                                                                                                                                                                                    • Can't you just do var counterInt = counter.map { Int($0) } ? Where counter whould be a String?

                                                                                                                                                                                      – Martin
                                                                                                                                                                                      Oct 19 '17 at 16:34











                                                                                                                                                                                    • @Martin - No. ? makes its optional and thus adds the word "optional" to the counter string.

                                                                                                                                                                                      – Sam B
                                                                                                                                                                                      Oct 19 '17 at 22:27











                                                                                                                                                                                    • IMHO, you should not force unwrap your optionals. Prefer use guard and if let statements

                                                                                                                                                                                      – Martin
                                                                                                                                                                                      Oct 20 '17 at 7:56



















                                                                                                                                                                                    • Can't you just do var counterInt = counter.map { Int($0) } ? Where counter whould be a String?

                                                                                                                                                                                      – Martin
                                                                                                                                                                                      Oct 19 '17 at 16:34











                                                                                                                                                                                    • @Martin - No. ? makes its optional and thus adds the word "optional" to the counter string.

                                                                                                                                                                                      – Sam B
                                                                                                                                                                                      Oct 19 '17 at 22:27











                                                                                                                                                                                    • IMHO, you should not force unwrap your optionals. Prefer use guard and if let statements

                                                                                                                                                                                      – Martin
                                                                                                                                                                                      Oct 20 '17 at 7:56

















                                                                                                                                                                                    Can't you just do var counterInt = counter.map { Int($0) } ? Where counter whould be a String?

                                                                                                                                                                                    – Martin
                                                                                                                                                                                    Oct 19 '17 at 16:34





                                                                                                                                                                                    Can't you just do var counterInt = counter.map { Int($0) } ? Where counter whould be a String?

                                                                                                                                                                                    – Martin
                                                                                                                                                                                    Oct 19 '17 at 16:34













                                                                                                                                                                                    @Martin - No. ? makes its optional and thus adds the word "optional" to the counter string.

                                                                                                                                                                                    – Sam B
                                                                                                                                                                                    Oct 19 '17 at 22:27





                                                                                                                                                                                    @Martin - No. ? makes its optional and thus adds the word "optional" to the counter string.

                                                                                                                                                                                    – Sam B
                                                                                                                                                                                    Oct 19 '17 at 22:27













                                                                                                                                                                                    IMHO, you should not force unwrap your optionals. Prefer use guard and if let statements

                                                                                                                                                                                    – Martin
                                                                                                                                                                                    Oct 20 '17 at 7:56





                                                                                                                                                                                    IMHO, you should not force unwrap your optionals. Prefer use guard and if let statements

                                                                                                                                                                                    – Martin
                                                                                                                                                                                    Oct 20 '17 at 7:56











                                                                                                                                                                                    0














                                                                                                                                                                                    Question : string "4.0000" can not be convert into integer using Int("4.000")?



                                                                                                                                                                                    Answer : Int() check string is integer or not if yes then give you integer and otherwise nil. but Float or Double can convert any number string to respective Float or Double without giving nil. Example if you have "45" integer string but using Float("45") gives you 45.0 float value or using Double("4567") gives you 45.0.



                                                                                                                                                                                    Solution : NSString(string: "45.000").integerValue or Int(Float("45.000")!)! to get correct result.






                                                                                                                                                                                    share|improve this answer




























                                                                                                                                                                                      0














                                                                                                                                                                                      Question : string "4.0000" can not be convert into integer using Int("4.000")?



                                                                                                                                                                                      Answer : Int() check string is integer or not if yes then give you integer and otherwise nil. but Float or Double can convert any number string to respective Float or Double without giving nil. Example if you have "45" integer string but using Float("45") gives you 45.0 float value or using Double("4567") gives you 45.0.



                                                                                                                                                                                      Solution : NSString(string: "45.000").integerValue or Int(Float("45.000")!)! to get correct result.






                                                                                                                                                                                      share|improve this answer


























                                                                                                                                                                                        0












                                                                                                                                                                                        0








                                                                                                                                                                                        0







                                                                                                                                                                                        Question : string "4.0000" can not be convert into integer using Int("4.000")?



                                                                                                                                                                                        Answer : Int() check string is integer or not if yes then give you integer and otherwise nil. but Float or Double can convert any number string to respective Float or Double without giving nil. Example if you have "45" integer string but using Float("45") gives you 45.0 float value or using Double("4567") gives you 45.0.



                                                                                                                                                                                        Solution : NSString(string: "45.000").integerValue or Int(Float("45.000")!)! to get correct result.






                                                                                                                                                                                        share|improve this answer













                                                                                                                                                                                        Question : string "4.0000" can not be convert into integer using Int("4.000")?



                                                                                                                                                                                        Answer : Int() check string is integer or not if yes then give you integer and otherwise nil. but Float or Double can convert any number string to respective Float or Double without giving nil. Example if you have "45" integer string but using Float("45") gives you 45.0 float value or using Double("4567") gives you 45.0.



                                                                                                                                                                                        Solution : NSString(string: "45.000").integerValue or Int(Float("45.000")!)! to get correct result.







                                                                                                                                                                                        share|improve this answer












                                                                                                                                                                                        share|improve this answer



                                                                                                                                                                                        share|improve this answer










                                                                                                                                                                                        answered Jan 4 '18 at 14:12









                                                                                                                                                                                        Ravi H MalviyaRavi H Malviya

                                                                                                                                                                                        769518




                                                                                                                                                                                        769518























                                                                                                                                                                                            0














                                                                                                                                                                                            I recently got the same issue. Below solution is work for me:



                                                                                                                                                                                                    let strValue = "123"
                                                                                                                                                                                            let result = (strValue as NSString).integerValue





                                                                                                                                                                                            share|improve this answer




























                                                                                                                                                                                              0














                                                                                                                                                                                              I recently got the same issue. Below solution is work for me:



                                                                                                                                                                                                      let strValue = "123"
                                                                                                                                                                                              let result = (strValue as NSString).integerValue





                                                                                                                                                                                              share|improve this answer


























                                                                                                                                                                                                0












                                                                                                                                                                                                0








                                                                                                                                                                                                0







                                                                                                                                                                                                I recently got the same issue. Below solution is work for me:



                                                                                                                                                                                                        let strValue = "123"
                                                                                                                                                                                                let result = (strValue as NSString).integerValue





                                                                                                                                                                                                share|improve this answer













                                                                                                                                                                                                I recently got the same issue. Below solution is work for me:



                                                                                                                                                                                                        let strValue = "123"
                                                                                                                                                                                                let result = (strValue as NSString).integerValue






                                                                                                                                                                                                share|improve this answer












                                                                                                                                                                                                share|improve this answer



                                                                                                                                                                                                share|improve this answer










                                                                                                                                                                                                answered Mar 16 '18 at 18:26









                                                                                                                                                                                                NirmalsinhNirmalsinh

                                                                                                                                                                                                3,27331437




                                                                                                                                                                                                3,27331437























                                                                                                                                                                                                    0














                                                                                                                                                                                                    An Int in Swift contains an initializer that accepts a String. It returns an optional Int? as the conversion can fail if the string contains not a number.



                                                                                                                                                                                                    By using an if let statement you can validate whether the conversion succeeded.



                                                                                                                                                                                                    So your code become something like this:



                                                                                                                                                                                                    @IBOutlet var txtBox1 : UITextField
                                                                                                                                                                                                    @IBOutlet var txtBox2 : UITextField
                                                                                                                                                                                                    @IBOutlet var txtBox3 : UITextField
                                                                                                                                                                                                    @IBOutlet var lblAnswer : UILabel

                                                                                                                                                                                                    @IBAction func btn1(sender : AnyObject) {

                                                                                                                                                                                                    let answer1 = "The acceleration is"
                                                                                                                                                                                                    var answer2 = txtBox1
                                                                                                                                                                                                    var answer3 = txtBox2
                                                                                                                                                                                                    var answer4 = txtBox3

                                                                                                                                                                                                    if let intAnswer = Int(txtBox1.text) {
                                                                                                                                                                                                    // Correctly converted
                                                                                                                                                                                                    }
                                                                                                                                                                                                    }





                                                                                                                                                                                                    share|improve this answer




























                                                                                                                                                                                                      0














                                                                                                                                                                                                      An Int in Swift contains an initializer that accepts a String. It returns an optional Int? as the conversion can fail if the string contains not a number.



                                                                                                                                                                                                      By using an if let statement you can validate whether the conversion succeeded.



                                                                                                                                                                                                      So your code become something like this:



                                                                                                                                                                                                      @IBOutlet var txtBox1 : UITextField
                                                                                                                                                                                                      @IBOutlet var txtBox2 : UITextField
                                                                                                                                                                                                      @IBOutlet var txtBox3 : UITextField
                                                                                                                                                                                                      @IBOutlet var lblAnswer : UILabel

                                                                                                                                                                                                      @IBAction func btn1(sender : AnyObject) {

                                                                                                                                                                                                      let answer1 = "The acceleration is"
                                                                                                                                                                                                      var answer2 = txtBox1
                                                                                                                                                                                                      var answer3 = txtBox2
                                                                                                                                                                                                      var answer4 = txtBox3

                                                                                                                                                                                                      if let intAnswer = Int(txtBox1.text) {
                                                                                                                                                                                                      // Correctly converted
                                                                                                                                                                                                      }
                                                                                                                                                                                                      }





                                                                                                                                                                                                      share|improve this answer


























                                                                                                                                                                                                        0












                                                                                                                                                                                                        0








                                                                                                                                                                                                        0







                                                                                                                                                                                                        An Int in Swift contains an initializer that accepts a String. It returns an optional Int? as the conversion can fail if the string contains not a number.



                                                                                                                                                                                                        By using an if let statement you can validate whether the conversion succeeded.



                                                                                                                                                                                                        So your code become something like this:



                                                                                                                                                                                                        @IBOutlet var txtBox1 : UITextField
                                                                                                                                                                                                        @IBOutlet var txtBox2 : UITextField
                                                                                                                                                                                                        @IBOutlet var txtBox3 : UITextField
                                                                                                                                                                                                        @IBOutlet var lblAnswer : UILabel

                                                                                                                                                                                                        @IBAction func btn1(sender : AnyObject) {

                                                                                                                                                                                                        let answer1 = "The acceleration is"
                                                                                                                                                                                                        var answer2 = txtBox1
                                                                                                                                                                                                        var answer3 = txtBox2
                                                                                                                                                                                                        var answer4 = txtBox3

                                                                                                                                                                                                        if let intAnswer = Int(txtBox1.text) {
                                                                                                                                                                                                        // Correctly converted
                                                                                                                                                                                                        }
                                                                                                                                                                                                        }





                                                                                                                                                                                                        share|improve this answer













                                                                                                                                                                                                        An Int in Swift contains an initializer that accepts a String. It returns an optional Int? as the conversion can fail if the string contains not a number.



                                                                                                                                                                                                        By using an if let statement you can validate whether the conversion succeeded.



                                                                                                                                                                                                        So your code become something like this:



                                                                                                                                                                                                        @IBOutlet var txtBox1 : UITextField
                                                                                                                                                                                                        @IBOutlet var txtBox2 : UITextField
                                                                                                                                                                                                        @IBOutlet var txtBox3 : UITextField
                                                                                                                                                                                                        @IBOutlet var lblAnswer : UILabel

                                                                                                                                                                                                        @IBAction func btn1(sender : AnyObject) {

                                                                                                                                                                                                        let answer1 = "The acceleration is"
                                                                                                                                                                                                        var answer2 = txtBox1
                                                                                                                                                                                                        var answer3 = txtBox2
                                                                                                                                                                                                        var answer4 = txtBox3

                                                                                                                                                                                                        if let intAnswer = Int(txtBox1.text) {
                                                                                                                                                                                                        // Correctly converted
                                                                                                                                                                                                        }
                                                                                                                                                                                                        }






                                                                                                                                                                                                        share|improve this answer












                                                                                                                                                                                                        share|improve this answer



                                                                                                                                                                                                        share|improve this answer










                                                                                                                                                                                                        answered Mar 1 at 15:50









                                                                                                                                                                                                        PatrickPatrick

                                                                                                                                                                                                        476313




                                                                                                                                                                                                        476313























                                                                                                                                                                                                            -1














                                                                                                                                                                                                            Convert String value to Integer in Swift 4



                                                                                                                                                                                                            let strValue:String = "100"
                                                                                                                                                                                                            let intValue = strValue as! Int
                                                                                                                                                                                                            var intValueFromString:Int = strValue as! Int
                                                                                                                                                                                                            or
                                                                                                                                                                                                            var intValueFromString = Int(strValue)!





                                                                                                                                                                                                            share|improve this answer






























                                                                                                                                                                                                              -1














                                                                                                                                                                                                              Convert String value to Integer in Swift 4



                                                                                                                                                                                                              let strValue:String = "100"
                                                                                                                                                                                                              let intValue = strValue as! Int
                                                                                                                                                                                                              var intValueFromString:Int = strValue as! Int
                                                                                                                                                                                                              or
                                                                                                                                                                                                              var intValueFromString = Int(strValue)!





                                                                                                                                                                                                              share|improve this answer




























                                                                                                                                                                                                                -1












                                                                                                                                                                                                                -1








                                                                                                                                                                                                                -1







                                                                                                                                                                                                                Convert String value to Integer in Swift 4



                                                                                                                                                                                                                let strValue:String = "100"
                                                                                                                                                                                                                let intValue = strValue as! Int
                                                                                                                                                                                                                var intValueFromString:Int = strValue as! Int
                                                                                                                                                                                                                or
                                                                                                                                                                                                                var intValueFromString = Int(strValue)!





                                                                                                                                                                                                                share|improve this answer















                                                                                                                                                                                                                Convert String value to Integer in Swift 4



                                                                                                                                                                                                                let strValue:String = "100"
                                                                                                                                                                                                                let intValue = strValue as! Int
                                                                                                                                                                                                                var intValueFromString:Int = strValue as! Int
                                                                                                                                                                                                                or
                                                                                                                                                                                                                var intValueFromString = Int(strValue)!






                                                                                                                                                                                                                share|improve this answer














                                                                                                                                                                                                                share|improve this answer



                                                                                                                                                                                                                share|improve this answer








                                                                                                                                                                                                                edited Dec 3 '18 at 17:14

























                                                                                                                                                                                                                answered Dec 3 '18 at 16:19









                                                                                                                                                                                                                Mahesh ChaudhariMahesh Chaudhari

                                                                                                                                                                                                                18714




                                                                                                                                                                                                                18714






























                                                                                                                                                                                                                    draft saved

                                                                                                                                                                                                                    draft discarded




















































                                                                                                                                                                                                                    Thanks for contributing an answer to Stack Overflow!


                                                                                                                                                                                                                    • Please be sure to answer the question. Provide details and share your research!

                                                                                                                                                                                                                    But avoid



                                                                                                                                                                                                                    • Asking for help, clarification, or responding to other answers.

                                                                                                                                                                                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                                                                                                                                                                                    To learn more, see our tips on writing great answers.




                                                                                                                                                                                                                    draft saved


                                                                                                                                                                                                                    draft discarded














                                                                                                                                                                                                                    StackExchange.ready(
                                                                                                                                                                                                                    function () {
                                                                                                                                                                                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24115141%2fconverting-string-to-int-with-swift%23new-answer', 'question_page');
                                                                                                                                                                                                                    }
                                                                                                                                                                                                                    );

                                                                                                                                                                                                                    Post as a guest















                                                                                                                                                                                                                    Required, but never shown





















































                                                                                                                                                                                                                    Required, but never shown














                                                                                                                                                                                                                    Required, but never shown












                                                                                                                                                                                                                    Required, but never shown







                                                                                                                                                                                                                    Required, but never shown

































                                                                                                                                                                                                                    Required, but never shown














                                                                                                                                                                                                                    Required, but never shown












                                                                                                                                                                                                                    Required, but never shown







                                                                                                                                                                                                                    Required, but never shown







                                                                                                                                                                                                                    Popular posts from this blog

                                                                                                                                                                                                                    MongoDB - Not Authorized To Execute Command

                                                                                                                                                                                                                    How to fix TextFormField cause rebuild widget in Flutter

                                                                                                                                                                                                                    Npm cannot find a required file even through it is in the searched directory