从解析类创建多个地图注释时出错

时间:2015-10-06 21:45:01

标签: ios swift parse-platform mapkit mapkitannotation

我尝试从Parse后端创建注释但我收到错误'[PFObject]?' is not convertible to '[PFObject]'

我的代码基于我在Query a GeoPoint from Parse and add it to MapKit as MKAnnotation?

找到的问题

下面是我的代码和错误的图片。 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)")
        }
    }

}

提前致谢

1 个答案:

答案 0 :(得分:0)

myMovers[PFObject]?PFObject的可选数组(可能是PFObjectnil的数组)。因为它是可选的,所以它不能直接转换为非可选项,因为您无法将nil转换为[PFObject]。所以你真正想要的是使用as?在这里进行条件转换并将其放在if let语句中。像这样

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

仅当movers[PFObject]而不是nil时才会执行大括号中的内容。