converting String it to a double but the value is nil (debugger says otherwise?)

123 views Asked by At

I am creating a map on Xcode via Swift where I am creating markers from an API and am about to take the longitude and latitude from the JSON and convert such into a double so I can get the CLLocation and display the marker. Yet when I am getting said information it appears that the longitude is converting fine as the latitude is returning an error saying:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value.

Normally I refer to the debugger but except it finds that the optional for the latitude is not only not nil but returns one of the values I'm trying to loop through: (lat String " -91.849071"). Yet despite this it's not fixing the error. Is there something I am missing? I have included my entire code from the swift file to show everything that I have done so I can show everything that is defining or not (sorry for the long paste)

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

There are 0 answers