Converting String to Int with Swift

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

    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  
            }
        }
    
    }
    

提交回复
热议问题