Could not cast value of type '__NSCFString' (0x10f0444a0) to 'NSNumber' (0x10e64c488).

Please help, I get the error: Could not cast value of type '__NSCFString' (0x10f0444a0) to 'NSNumber' (0x10e64c488).

I am trying to loop through all my Latitudes and Longitudes and plot pins on a map

I think it may have someting to do with the fact that they are Strings?!

I am using Firebase so therefore dictionaries


for child in snapDict{
                      
                        let shotKey = snapshot.children.nextObject() as! FIRDataSnapshot
                      
                        if let name = child.value as? [String:AnyObject]{
                          
                            var latitude = name["vLat"] as! Int32
                            var longitude = name["vLon"] as! Int32
                          
                            let distancespan:CLLocationDegrees = 5000
                            let theLocations:CLLocationCoordinate2D = CLLocationCoordinate2DMake(CLLocationDegrees(latitude), CLLocationDegrees(longitude))
                            self.mapView.setRegion(MKCoordinateRegionMakeWithDistance(theLocations, distancespan, distancespan), animated: true)
                          
                            let worthingClassPin = mapAnnotation(title: "This is Worthing", subtitle: "Worthing Map", coordinate: theLocations)
                          
                            self.mapView.addAnnotation(worthingClassPin)
                            /
                            /
                          
                        }
                      
                    }

Please, be more precise in the description of your problems. Where exactly do you get the message (which line) ?


I assume it is in


var latitude = name["vLat"] as! Int32
var longitude = name["vLon"] as! Int32


This cannot work, as a String cannot be cast to an Int, but can be converted to.

if name is an Array of String (how is it declared ?), then you should write :

Are you sure that latitude is an Int ? Or not a Double ?


Depending, you could write :

var latitude = Int(name["vLat"]) ?? 0    
or
var latitude = Double(name["vLat"]) ?? 0.0

Thanks, so my code now reads as this:


if let name = child.value as? [String:AnyObject]{
                           
                            var latitude = Double(name["vLat"]) ?? 0.0
                            let longitude = Double(name["vLat"]) ?? 0.0



I am now getting: Cannot invoke initializer of type 'Double' with an argiument list of type (AnyObject?)

Question is: what is the type of name.


Try with :


if let latName = name["vLat"] as? String {
     let latitude = Double(latName) ?? 0.0
}

Thank you.... it's kind of making sense now but it's one thing after another... so, here is my code:


if let name = child.value as? [String:AnyObject]{
                            if let latName = name["vLat"] as? String {
                                let latitude = Double(latName) ?? 0.0
                            }
                            if let lonName = name["vLon"] as? String {
                                let longitude = Double(lonName) ?? 0.0
                            }
                          
                            let distancespan:CLLocationDegrees = 5000
                            let theLocations:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
                            self.mapView.setRegion(MKCoordinateRegionMakeWithDistance(theLocations, distancespan, distancespan), animated: true)
                          
                            let worthingClassPin = mapAnnotation(title: "This is Worthing", subtitle: "Worthing Map", coordinate: theLocations)
                          
                            self.mapView.addAnnotation(worthingClassPin)
                          
                          }


I am now getting: Use of unreolved itentifyer 'latitude' on the following line:

let theLocations:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)

OK so you should study the basic scope rules a bit - if you define latitude within the braces of an if let clause then it can only be used within those braces. Given that you will only want to move forward if the conversions are successful you should write something like:


if let latName = name["vLat"] as? String,
     let lonName = name["vLon"] as? String {
     let latitude = Double(latName) ?? 0.0
     let longitude = Double(lonName) ?? 0.0
     //And the rest of your code using latitude and longitude...
}

Or you can declare a var at the correct scope level:


                         if let name = child.value as? [String:AnyObject] {
                            var longitude : Double = 0.0
                            var latitude : Double = 0.0
                            if let latName = name["vLat"] as? String {
                                latitude = Double(latName) ?? 0.0
                            }
                            if let lonName = name["vLon"] as? String {
                                longitude = Double(lonName) ?? 0.0
                            }
                        
                            let distancespan:CLLocationDegrees = 5000
                            let theLocations:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
                            self.mapView.setRegion(MKCoordinateRegionMakeWithDistance(theLocations, distancespan, distancespan), animated: true)
                        
                            let worthingClassPin = mapAnnotation(title: "This is Worthing", subtitle: "Worthing Map", coordinate: theLocations)
                        
                            self.mapView.addAnnotation(worthingClassPin)
                          }
Could not cast value of type '__NSCFString' (0x10f0444a0) to 'NSNumber' (0x10e64c488).
 
 
Q