从解析创建多个地图注释时出错 class
Error creating multiple map annotations from a parse class
我正在尝试从 Parse 后端创建注释,但出现错误 '[PFObject]?' is not convertible to '[PFObject]'
我的代码基于我在这里找到的一个问题
这是我的代码和错误的图片。 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
数组(可能是 PFObject
数组或 nil
数组)。因为它是可选的,所以它不能直接转换为非可选的,因为您不能将 nil
转换为 [PFObject]
。所以你真正想要的是在这里使用 as?
进行条件转换并将其放入 if let
语句中。像这样
if let myMovers = movers as? [PFObject] {
// Use myMovers to do what you want
}
仅当 movers
是 [PFObject]
而不是 nil
时才会执行大括号中的内容。
我正在尝试从 Parse 后端创建注释,但出现错误 '[PFObject]?' is not convertible to '[PFObject]'
我的代码基于我在这里找到的一个问题
这是我的代码和错误的图片。 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
数组(可能是 PFObject
数组或 nil
数组)。因为它是可选的,所以它不能直接转换为非可选的,因为您不能将 nil
转换为 [PFObject]
。所以你真正想要的是在这里使用 as?
进行条件转换并将其放入 if let
语句中。像这样
if let myMovers = movers as? [PFObject] {
// Use myMovers to do what you want
}
仅当 movers
是 [PFObject]
而不是 nil
时才会执行大括号中的内容。