Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert optional string to double in Swift 3

Tags:

ios

swift

swift3


I have a option string and want to convert that to double.
this worked in Swift 2 , but since converted to Swift 3, I am getting value of 0.

var dLati = 0.0
dLati = (latitude as NSString).doubleValue

I have check and latitude has a optional string value of something like -80.234543218675654 , but dLati value is 0

*************** ok, new update for clarity *****************
I have a viewcontroller which i have a button in it, and when the button is touched, it will call another viewcontroller and pass a few values to it here is the code for the first viewcontroller

var currentLatitude: String? = ""
var currentLongitude: String? = ""
var deviceName = ""
var address = ""
// somewhere in the code, currentLatitude and currentLongitude are get set  
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "map" {
       let destViewController : MapViewController = segue.destination as! MapViewController
       print(currentLongitude!)  // Print display: Optional(-80.192279355363768)
       print(currentLatitude!) // Print display: Optional(25.55692663937162) 
       destViewController.longitude = currentLongitude!
       destViewController.latitude = currentLatitude!
       destViewController.deviceName = deviceName
       destViewController.address = address
    }
}

Here is the code for the second view controller called MapViewController

   var longitude: String? = " "
   var latitude: String? = ""
   .
   .
   override func viewDidLoad() {
       if let lat = latitude {
          print(lat) // Print display: optiona(25.55692663937162)
          dLati = (lat as NSString).doubleValue
          print(dLati)  // Print display: 0.0
       }
       .
       .
   }

Thanks Borna

like image 387
borna Avatar asked Nov 02 '16 04:11

borna


People also ask

How do I convert a string to a double in Swift?

Convert Swift String to DoubleUse Double , Float , CGFloat to convert floating-point values (real numbers). let lessPrecisePI = Float("3.14") let morePrecisePI = Double("3.1415926536") let width = CGFloat(Double("200.0")!)

How do I convert a number to a double in Swift?

To convert a string to a double, we can use the built-in Double() initializer syntax in Swift. The Double() initializer takes the string as an input and returns the double instance. If the string contains invalid characters or invalid format it returns nil.


2 Answers

A safe way to achieve this without needing to use Foundation types is using Double's initializer:

if let lat = latitude, let doubleLat = Double(lat) {
  print(doubleLat)  // doubleLat is of type Double now
}
like image 199
BJ Miller Avatar answered Oct 02 '22 07:10

BJ Miller


Unwrap the latitude value safely and then use

var dLati = 0.0

if let lat = latitude {
    dLati = (lat as NSString).doubleValue
}
like image 25
Rajan Maheshwari Avatar answered Oct 02 '22 05:10

Rajan Maheshwari