如何将我在 JSON 中获得的信息转换为 DOUBLE 以便我可以正确使用这些信息?

How do I transform the info I got in JSON into a DOUBLE so I can use the info correctly?

The error that occured

The structure I put the data in

The function call that got the json information in the first place

The information sent into another structure

我正在使用智能汽车 api 编写联网汽车应用程序。我在 heroku 上有一个 node.js 后端 运行ning,在 ipad.

上有一个 运行 前端 swift 4

当我在 ipad 上将应用程序编译为 运行 时,运行 没问题。 它通过身份验证过程,但发生的是我收到此错误

线程 1:EXC_BREAKPOINT(代码=1,子代码=0x100f16054)

第一个就在这里

var carLatitude:Double = carInfo.latitude as!双

var carLongitude:Double = carInfo.longitude as!双

正如您从下面的代码中看到的,我创建了一个名为代码数据的结构。然后在 " Alamofire.request("(Constants.appServer)/location", " 我得到一个 json 文档并将其保存到名为 "teslaCar"

的结构 carData 的实例中

然后我将 "teslaCar" 保存到 "carInfo" 结构以将其转移到另一个视图。

然后我尝试简单地将 carInfo.latitude 和 carInfo.longitude 变成一个变量并将其变成一个双精度变量。我在将纬度和经度转换为双精度时遇到了世界上最困难的麻烦。传输正确后,我会在mapkit上展示。

谁能告诉我如何进行这项工作?

    import Alamofire
import UIKit
import SmartcarAuth
import SwiftyJSON

struct carData {
    var make: String = ""
    var model: String = ""
    var year: String = ""
    var latitude: String = ""
    var longitude: String = ""
    var odometer: String = ""
}


            Alamofire.request("\(Constants.appServer)/location", method: .get).responseJSON { response in
                switch response.result {
                case .success(let value):
                    let json = JSON(value)
                    print("JSON: \(json)")

                    let latitude = json["data"]["latitude"].stringValue
                    let longitude = json["data"]["longitude"].stringValue
                    let locationDeclaration = "Latitude: \(latitude) and Longitude: \(longitude)"
                    self.vehicleLocationText = locationDeclaration
                    print(json["data"]["latitude"])
                    print(json["data"]["longitude"])

                    self.teslaCar.latitude = latitude
                    self.teslaCar.longitude = longitude
                  /*   self.performSegue(withIdentifier: "displayVehicleInfo", sender: self)
 */
                case .failure(let error):
                    print(error)
                }


            }

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if var destinationVC = segue.destination as? InfoViewController {
            destinationVC.carInfo = teslaCar

        }



       var carLatitude:Double = carInfo.latitude as! Double
        var carLongitude:Double = carInfo.longitude as! Double

请仔细阅读错误信息。

Cast from String to unrelated type Double always fails.

如果您不能将一种类型转换为另一种类型,您必须使用初始化方法转换该类型。

请同时阅读黄色警告并考虑将未突变的 variables 声明为常量的建议

let carLatitude = Double(carInfo.latitude) ?? 0.0
let carLongitude = Double(carInfo.longitude) ?? 0.0

如果字符串值不能转换为Double

?? 0.0部分赋值0.0

有关类型转换的更多信息,请阅读The Basics in Swift Language Guide

This is the fixed version of the code with the changes that @vadian made. It now works This is the map function that I wrote

This is a screen shot of the operational program from the iPad thanks to @vadian's help

答案就是写这段代码

       let carLatitude = Double(carInfo.latitude) ?? 0.0
    let carLongitude = Double(carInfo.longitude) ?? 0.0

        let initialLocation = CLLocation(latitude: carLatitude ?? 0.0, longitude: carLongitude ?? 0.0)

不仅需要使用方法并将我尝试转换的变量作为参数发送,而且还需要添加 ?? 0.0

然后当我将我创建的变量传递给名为 "CLLocation" 的 MapKit 函数时,有必要添加一个 ?? 0.0

出于某种原因,这就是你如何让被认为是 Any 类型的东西成为 Any

这就是我所能描述的答案。有更好的方式来解释它,这是我能说的最好的。