-2

Attached is my code, it is basically two text boxes being added together, however I do not know how to handle float variables. Any help is appreciated

@IBOutlet weak var first: UITextField!
@IBOutlet weak var second: UITextField!
@IBOutlet weak var total: UILabel!

@IBAction func add(sender: AnyObject) {

   total.text = "\(first.text.toInt()!+second.text.toInt()!)"
JMac
  • 1

1 Answers1

1

You could try

let firstFloatValue = (first?.text as NSString).floatValue
let secondFloatValue = (second?.text as NSString).floatValue

and then,

total.text = "\(firstFloatValue + secondFloatValue)

Or any one of the many techniques mentioned in Convert String to float in Apple's Swift

Community
  • 1
  • 1
gran_profaci
  • 8,087
  • 15
  • 66
  • 99