我有一个选项字符串,想将其转换为双精度。
这在 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
}
.
.
}
谢谢
博尔纳
无需使用 Foundation 类型即可实现此目的的安全方法是使用 Double 的初始值设定项:
if let lat = latitude, let doubleLat = Double(lat) {
print(doubleLat) // doubleLat is of type Double now
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)