Converting String to Int with Swift

前端 未结 30 1272
小鲜肉
小鲜肉 2020-11-22 06:59

The application basically calculates acceleration by inputting Initial and final velocity and time and then use a formula to calculate acceleration. However, since the value

相关标签:
30条回答
  • 2020-11-22 07:01

    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
    
    0 讨论(0)
  • 2020-11-22 07:02

    Swift5 float or int string to int:

    extension String {
        func convertStringToInt() -> Int {
            return Int(Double(self) ?? 0.0)
        }
    }
    
    let doubleStr = "4.2"
    // print 4
    print(doubleStr.convertStringToInt())
    
    let intStr = "4"
    // print 4
    print(intStr.convertStringToInt())
    
    0 讨论(0)
  • 2020-11-22 07:02

    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
        }
    }
    
    0 讨论(0)
  • 2020-11-22 07:03

    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 
      }
    
    0 讨论(0)
  • 2020-11-22 07:03

    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.

    0 讨论(0)
  • 2020-11-22 07:04

    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
    
    0 讨论(0)
提交回复
热议问题