Converting String to Int with Swift

前端 未结 30 1287
小鲜肉
小鲜肉 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: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 
      }
    

提交回复
热议问题