Converting String to Int with Swift

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

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

提交回复
热议问题