So, I have looked at all the other questions regarding this and I've attempted to implement the suggested solutions, but to no avail. I have two text fields in a view controller that a user enters numbers into and is then allowed to perform either an addition or subtraction of the two numbers. Unless I'm mistaken, the text fields are strings and need to be converted to integers before the calculation can be done. I'm not having any luck with that conversion though. Any suggestions in the right direction would be much appreciated. Here is the applicable code:
let firstInput : Int? = Int(firstNumber)
let secondInput : Int? = Int(secondNumber)
var result = 0
@IBOutlet weak var firstNumber: UITextField!
@IBOutlet weak var secondNumber: UITextField!
@IBOutlet weak var segmentedControlCalculate: UISegmentedControl!
@IBAction func segmentedControlAddSubtract(sender: AnyObject) {
switch segmentedControlCalculate.selectedSegmentIndex {
case 0:
result = firstInput+ secondInput
case 1:
result = firstInput - secondInput
default:
return
}
updateTotalLabel()
}
func updateTotalLabel() {
total.text = "Total = " + String(result)
}
@IBOutlet weak var total: UILabel!
@IBAction func dismiss(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}