将String转换为double,但值为nil(调试器是否另外说明?)

时间:2019-04-11 02:21:32

标签: ios swift xcode-debugger

我正在通过Swift在Xcode上创建地图,在那我要通过API创建标记,并且要从JSON获取经度和纬度并将其转换为双精度,因此我可以获取CLLocation并显示标记。但是,当我得到上述信息时,由于纬度返回一个错误,表明经度转换正常:

  

线程1:致命错误:展开一个Optional值时意外发现nil。

通常我指的是调试器,但它发现纬度的可选值不仅为nil,而且还返回我尝试循环通过的值之一:(lat字符串“ -91.849071”)。尽管如此,它仍无法解决错误。我有什么想念的吗?我已经从swift文件中包含了我的整个代码,以显示我已完成的所有操作,因此我可以显示是否已定义的所有内容(很长的粘贴内容很抱歉)

import Foundation
import CoreLocation
import MapKit

class apiInfo: NSObject, MKAnnotation {
    var city = ""
    var name  = ""
    var state = ""
    var id = ""
    //var location : CLLocation?
    var longitude = 0.0
    var latitude = 0.0
    //needed for the MKAnnotation protocol
    var coordinate: CLLocationCoordinate2D {
        get {
            let location = CLLocation(latitude:latitude, longitude:longitude)
            return location.coordinate
        }
    }

    //DEFINING VARIABLES

    override var description : String {
        return "{\n\t Landmark Name: \(name)\n\t State: \(id)\n\t id: \(state)\n\t location: \(city)\n\t city: \(String(describing: longitude))\nm\(String(describing: latitude))\nm}"
    }

    override  var debugDescription: String {
        return "{\n\t Landmark Name: \(name)\n\t State: \(id)\n\t id: \(state)\n\t location: \(city)\n\t city: \(String(describing: longitude))\nm\(String(describing: latitude))\nm}"
    }

    //CONSTRUCTORS

    init(name: String, state: String, city: String, id: String, latitude: Double, longitude: Double) {
        self.name = name
        self.city = city
        self.state = state
        self.id = id
        self.latitude = latitude
        self.longitude = longitude
    }

    //optional - required with set callout true
    var title : String? {
        get {
            return name
        }
    }

    var subtitle : String? {
        get {
            return state
        }
    }

    //Mutators

    init(name:String, city:String, state:String, id:String, latitude: Double, longitude: Double, location: CLLocation ) {
        super.init()
        self.name = name
        self.city = city
        self.state = state
        self.id = id
        self.longitude = longitude
        self.latitude = latitude

    }

    //MUTATORS

    init(json:[String:Any]) {
        //print("##### \(json)")
        if let n = json["Name"] as? String {
            name =  n
        }
        if let c = json["City"] as? String {
            city = c
        }
        if let s = json["State"] as? String {
            state = s
        }
        if let i = json["id"] as? String {
            id = i
        }
        if let lng = json["Long"] as? String {
            '''
                longitude = Double(lng)! //WORKS FINE
            '''

        }

        if let lat = json["Lat"] as? String {

                '''
                latitude = Double(lat)!
                ''' //RETURNING NIL

        }
    }

    //parsing from JSON
    // final convenience int
    override convenience init() {
        self.init(name: "Unknown", state: "Unknown", city:"Unknown" ,  id: "Unknown", latitude: 0.0, longitude: 0.0)
    }
}

0 个答案:

没有答案