我尝试从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)")
}
}
}
提前致谢
答案 0 :(得分:0)
myMovers
是[PFObject]?
,PFObject
的可选数组(可能是PFObject
或nil
的数组)。因为它是可选的,所以它不能直接转换为非可选项,因为您无法将nil
转换为[PFObject]
。所以你真正想要的是使用as?
在这里进行条件转换并将其放在if let
语句中。像这样
if let myMovers = movers as? [PFObject] {
// Use myMovers to do what you want
}
仅当movers
为[PFObject]
而不是nil
时才会执行大括号中的内容。