2

I'm brand new to Swift (quarantine learning) and was following along a YouTube video to create a total price app. I'm having trouble changing a string to a double type, basically taking the input and turning it into a double so I can do math with it.

Here is my code:

import UIKit


class ViewController: UIViewController {

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

    @IBOutlet weak var pricetxt: UITextField!
    @IBOutlet weak var taxtxt: UITextField!
    @IBOutlet weak var totalpricelbl: UILabel!

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

    @IBAction func Calculate(sender: AnyObject) {
        let price = Double(pricetxt.text!)!
        let tax = Double(taxtxt.text!)!

        let totalsalestax = price * tax
        let totalprice = price + totalsalestax
        totalpricelbl.text = "$\(totalprice)"
    }

}

I get an error with these two lines:

let price = Double(pricetxt.text!)!
let tax = Double(taxtxt.text!)!

saying:

"Cannot invoke 'init' with an argument of type '@!value String!'

Running on my Mac 10.9.5 XCode is 6.2 I know these are both old but I can't upgrade my laptop.

Any help is appreciated!

NicolasElPapu
  • 1,285
  • 1
  • 7
  • 22
Jacob3454
  • 163
  • 6
  • Have you connected your text fields and label properly to the IBOutlet in your storyboard? – Joakim Danielson May 17 '20 at 20:49
  • What version of Swift? – elliott-io May 17 '20 at 20:58
  • @JoakimDanielson yup! all fields are connected properly – Jacob3454 May 17 '20 at 21:15
  • @elliott-io i'm using this link to check what version https://stackoverflow.com/questions/30790188/how-do-i-see-which-version-of-swift-im-using/50821163 however 'swift language' menu does not appear when I run it on my Xcode 6.2. I'm guessing because it's too old? – Jacob3454 May 17 '20 at 21:16
  • Oi, ok. You could try `Double.init(pricetxt.text)`... – elliott-io May 17 '20 at 21:47
  • If you are using Swift 1.x try `(pricetxt.text! as NSString).doubleValue` – Leo Dabus May 17 '20 at 22:01
  • 1
    Note that Swift have changed a lot since then. It will be probably a waste of time to learn Swift 1.x. Xcode 6 also was really problematic at that time. What is your machine? – Leo Dabus May 17 '20 at 22:06
  • As an aside, we might be able to cut the Gordian knot. When converting user input from a string to a floating point number, you shouldn’t use `Double` initializer, anyway, as that’s not localized. Nowadays we’d use `NumberFormatter` (but for Swift 1.1, it was still called `NSNumberFormatter`). Number formatters properly convert user input to numeric format for the locale of the device, but neither the `Double` initializer nor the `doubleValue` method will. – Rob May 18 '20 at 04:24

1 Answers1

1

Try this out:

let price = (pricetxt.text! as NSString).doubleValue
let tax = (taxtxt.text! as NSString).doubleValue
Mallard
  • 385
  • 2
  • 11