0

I'm trying to create annotations from Parse backend but I get an error '[PFObject]?' is not convertible to '[PFObject]'

I based my code on a question i found here Query a GeoPoint from Parse and add it to MapKit as MKAnnotation?

Heres a pic of my code and the error. code photo

{

mapView.showsUserLocation = true
mapView.delegate = self
mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
MapViewLocationManager.delegate = self
MapViewLocationManager.startUpdatingLocation()


    var annotationQuery = PFQuery(className: "Movers")
     currentLoc = PFGeoPoint(location: MapViewLocationManager.location)
    annotationQuery.whereKey("ubicacion", nearGeoPoint: currentLoc, withinKilometers: 10)
    annotationQuery.findObjectsInBackgroundWithBlock {
        (movers, error) -> Void in
        if error == nil {
            // The find succeeded.
            print("Successful query for annotations")

            let myMovers = movers as [PFObject]

                for mover in myMovers {
                let point = movers["ubicacion"] as PFGeoPoint
                let annotation = MKPointAnnotation()
                annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
                self.mapView.addAnnotation(annotation)

        }

        }else {
            // Log details of the failure
            print("Error: \(error)")
        }
    }

}

Thanks in advance

Community
  • 1
  • 1

1 Answers1

0

myMovers is a [PFObject]?, an optional array of PFObjects (as in maybe an array of PFObjects or maybe nil). Because it is an optional, it's not directly convertible to the non optional because you can't convert nil to [PFObject]. So what you really want is to do a conditional cast here using as? and put it in an if let statement. Like so

if let myMovers = movers as? [PFObject] {
    // Use myMovers to do what you want 
}

That will execute what is in the braces only when movers is a [PFObject] and not nil.

Scott H
  • 1,509
  • 10
  • 14