我尝试从 Parse 后端创建注释,但收到错误 '[PFObject]?'无法转换为“[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)")
}
}
}
提前致谢
最佳答案
myMovers
是 [PFObject]?
,可选数组 PFObject
s (可能是 PFObject
的数组,或者可能是 nil
)。因为它是可选的,所以它不能直接转换为非可选,因为您无法转换 nil
至[PFObject]
。所以你真正想要的是在这里使用 as?
进行条件转换并将其放入 if let
陈述。就像这样
if let myMovers = movers as? [PFObject] {
// Use myMovers to do what you want
}
只有当 movers
时才会执行大括号中的内容是 [PFObject]
而不是nil
.
关于ios - 从解析类创建多个 map 注释时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32980295/