我有一个选项字符串,想要将其转换为double类型。
在Swift 2中可以工作,但自从转换为Swift 3后,我得到的值为0。
var dLati = 0.0
dLati = (latitude as NSString).doubleValue
我检查了一下,纬度的可选字符串值可能类似于-80.234543218675654,但dLati的值为0。
*************** 好的,为了更清晰更新 *****************
我有一个视图控制器,在其中有一个按钮,当点击该按钮时,它会调用另一个视图控制器并传递一些值。以下是第一个视图控制器的代码:
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
}
}
这里是第二个视图控制器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
}
.
.
}
谢谢 Borna
latitude
是从哪里来的?它是如何声明的?它保证始终具有非 nil 的有效数字值吗? - rmaddy