-2

Apologies for the elementary question, I'm new to swift and I've been stuck at this for a while but haven't found help.

I'm trying to perform simple math operations like addition, multiplication and division etc. in my iOS app but haven't been able to.

When I try to add two double numbers (weightField and heightField), I get a concatenated string instead of a sum.

How do I perform simple math in swift?

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var weightField: UITextField!

    @IBOutlet weak var heightField: UITextField!

    @IBAction func goButton(sender: AnyObject) {

        resultField.text = weightField.text + heightField.text
    }

    @IBOutlet weak var resultField: UILabel!

    @IBOutlet weak var commentField: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
Emzor
  • 1,380
  • 17
  • 28

3 Answers3

2

You don't add the values of the strings together so if you are sure that text is convertible to an Int you can do something like this:

// in Swift 1.x
resultField.text = String(weightField.text.toInt()! + heightField.text.toInt()!)

// and double values
let weight = (weightField.text as NSString).doubleValue
let height = (heightField.text as NSString).doubleValue
resultField.text = String(weight + height)
// but if it cannot parse the String the value is 0.0. (No optional value)


// in Swift 2
resultField.text = String(Int(weightField.text)! + Int(heightField.text)!)

// and you can even use a double initializer
resultField.text = String(Double(weightField.text)! + Double(heightField.text)!)
Qbyte
  • 12,753
  • 4
  • 41
  • 57
  • Thanks @ Qbyte, your solution worked well, I however forgot to mention that the numbers are Floats not Ints. Any suggestion? – Emzor Jul 22 '15 at 11:32
  • Using NSString: `(textField.text as NSString).doubleValue` but if it cannot parse it the value is 0.0. (No optional value) – Qbyte Jul 22 '15 at 11:38
  • I appreciate your help, but I don't quite understand. Please could you explain further on how to implement NSString? – Emzor Jul 22 '15 at 12:05
  • `NSString` is a class of Foundation which you can use right away since you imported UIKit already. – Qbyte Jul 22 '15 at 12:07
1

Use NSString:

resultField.text = String(stringInterpolationSegment: (weightField.text as NSString).doubleValue + (heightField.text as NSString).doubleValue)
Hamza Ansari
  • 3,009
  • 1
  • 23
  • 25
0

Basically you are concatenating the strings not number. You need to convert string into integer and then add those.

var a = weightField.text

var b =  heightField.text

var c = (a as! NSString).doubleValue +  (b as! NSString).doubleValue

resultField.text = String(format "%.2f",c)
iAnurag
  • 9,286
  • 3
  • 31
  • 48
  • Thanks @iAnurag, I had to do unwrap like this var a : Int = weightField.text.toInt()! + heightField.text.toInt()! Worked well, I however forgot to mention that the numbers are Floats not Ints. Any suggestion? – Emzor Jul 22 '15 at 11:31
  • I tried this approach, and I understand the logic. But for some strange reason(s), I'm getting a compile error. Not sure why. :-( – Emzor Jul 22 '15 at 13:12