Converting String to Int with Swift

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

    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
    

提交回复
热议问题