Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find an initializer for type Double in Swift

I would like to convert the String value I get from a text field into a Double value and store it but I keep getting the error that it cannot find an intializer for type Double that accepts an argument list type of (String). How do I fix this issue?

 @IBOutlett weak var tempText: UITextField! 
 @IBAction func convertBtn(sender: AnyObject){
     let t = Double(tempText.text!) 
     let temp = TempCoverterModel(temp:t!)
     tempText.text = String(temp.toCelsius())
}
like image 463
Ashley Avatar asked Nov 18 '15 01:11

Ashley


2 Answers

Use an NSNumberFormater:

let formatter = NSNumberFormatter()
let t = formatter.numberFromString(tempText.text!)!.doubleValue
like image 67
Code Different Avatar answered Oct 12 '22 02:10

Code Different


If you have a String named temp, you should be able to use

(temp as NSString).doubleValue 

to convert into a Double value.

like image 23
hiimcg Avatar answered Oct 12 '22 02:10

hiimcg