[英]Convert optional string to double in Swift 3
我有一個選項字符串,並希望將其轉換為double。
這在Swift 2中有效,但自從轉換為Swift 3后,我的值為0。
var dLati = 0.0
dLati = (latitude as NSString).doubleValue
我有檢查和緯度有一個可選的字符串值,如-80.234543218675654,但dLati值為0
***************好的,為了清晰起見的新更新*****************
我有一個viewcontroller,我有一個按鈕,當觸摸按鈕時,它將調用另一個viewcontroller並傳遞一些值到這里是第一個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
}
}
這是第二個視圖控制器的代碼,稱為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
}
安全打開latitude
值然后使用
var dLati = 0.0
if let lat = latitude {
dLati = (lat as NSString).doubleValue
}
let dLati = Double(latitude ?? "") ?? 0.0
這段代碼工作正常。
var dLati = 0.0
let latitude: String? = "-80.234543218675654"
if let strLat = latitude {
dLati = Double(strLat)!
}
當你得到一個像這樣的雙值的字符串
"Optional(12.34567)"
您可以使用Regex從字符串中取出double值。 如果字符串是"Optional(12.34567)"
這是Regex的示例代碼:
let doubleLatitude = location.latitude?.replacingOccurrences(of: "[^\\.\\d+]", with: "", options: [.regularExpression])
您可以在一行中完成此操作。
var latitude: Double = Double("-80.234543218675654") ?? 0.0
這將創建一個名為latitude的變量,其類型為Double,可以使用成功的Double from String進行實例化,或者返回值為0.0
不要將它轉換為NSString
,你可以將它強制為Double
但如果失敗則會有后備。 像這樣的東西:
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
所以在你的情況下:
dLati = Double(latitude!) ?? 0.0
更新:要處理nil
值,請執行以下操作(請注意,讓cLat為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")
}
實際上,Optional這個詞是字符串的一部分。 不確定它是如何添加到字符串中的? 但我修復它的方式就是這樣。 latitude是這個字符串“Optional(26.33691567239162)”然后我做了這個代碼
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)
並將此作為最終值
26.33691567239162
在swift 3.1中 ,我們可以組合擴展和Concrete Constrained Extensions
extension Optional where Wrapped == String
{
var asDouble: Double
{
return NSString(string: self ?? "").doubleValue
}
}
要么
extension Optional where Wrapped == String
{
var asDouble: Double
{
return Double(str ?? "0.00") ?? 0.0
}
}
斯威夫特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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.