Convert optional string to double in Swift 3

2019-06-15 00:19发布


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

标签: ios swift swift3
10条回答
老娘就宠你
2楼-- · 2019-06-15 00:47
let dLati = Double(latitude ?? "") ?? 0.0
查看更多
祖国的老花朵
3楼-- · 2019-06-15 00:48

Don´t convert it to an NSString, you can force it to a Double but have a fallback if it fails. Something like this:

let aLat: String? = "11.123456"
let bLat: String? = "11"
let cLat: String? = nil

let a = Double(aLat!) ?? 0.0 // 11.123456
let b = Double(bLat!) ?? 0.0 // 11
let c = Double(cLat!) ?? 0.0 // 0

So in your case:

dLati = Double(latitude!) ?? 0.0

Update: To handle nil values do the following (note that let cLat is nil:

// Will succeed
if let a = aLat, let aD = Double(aLat!) {
    print(aD)
}
else {
    print("failed")
}

// Will succeed
if let b = bLat, let bD = Double(bLat!) {
    print(bD)
}
else {
    print("failed")
}

// Will fail
if let c = cLat, let cD = Double(cLat!) {
    print(cD)
}
else {
    print("failed")
}
查看更多
We Are One
4楼-- · 2019-06-15 00:51

Unwrap the latitude value safely and then use

var dLati = 0.0

if let lat = latitude {
    dLati = (lat as NSString).doubleValue
}
查看更多
劳资没心,怎么记你
5楼-- · 2019-06-15 00:56

Swift 4

 let YourStringValue1st = "33.733322342342" //The value is now in string
 let YourStringValue2nd = "73.449384384334" //The value is now in string

 //MARK:- For Testing two Parameters 
 if let templatitude = (YourStringValue1st as? String), let templongitude = (YourStringValue2nd as? String)
 {
     movetosaidlocation(latitude: Double(templat)!, longitude: Double(templong)!, vformap: cell.vformap)
 }


 let YourStringValue = "33.733322342342" //The value is now in string
 //MARK:- For Testing One Value
 if let tempLat = (YourStringValue as? String)
 {
      let doublevlue = Double(tempLat)
      //The Value is now in double (doublevlue)
 }
查看更多
我命由我不由天
6楼-- · 2019-06-15 00:57

Actually the word optional was part of the string. Not sure how it got added in the string? But the way I fixed it was like this. latitude was this string "Optional(26.33691567239162)" then I did this code

let start = latitude.index(latitude.startIndex, offsetBy: 9) 
let end = latitude.index(latitude.endIndex, offsetBy: -1) 
let range = start..<end 
latitude = latitude.substring(with: range) 

and got this as the final value
26.33691567239162

查看更多
闹够了就滚
7楼-- · 2019-06-15 00:58

This code works fine.

var dLati = 0.0
let latitude: String? = "-80.234543218675654"
if let strLat = latitude {
    dLati = Double(strLat)!
}
查看更多
登录 后发表回答