首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从解析类创建多个映射注释时出错

从解析类创建多个映射注释时出错
EN

Stack Overflow用户
提问于 2015-10-06 21:45:01
回答 1查看 51关注 0票数 0

我正在尝试从Parse后端创建注释,但是我得到了一个错误'[PFObject]?' is not convertible to '[PFObject]'

我的代码是基于我在这里找到的一个问题,从解析中查询GeoPoint并将其作为MKAnnotation添加到MapKit中?

这是我的代码和错误的图片。代码照片

代码语言:javascript
复制
{

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)")
        }
    }

}

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-06 23:29:47

myMovers是一个[PFObject]?,一个PFObject的可选数组(可能是PFObject的数组,也可能是nil的数组)。因为它是可选的,所以它不能直接转换为非可选的,因为您不能将nil转换为[PFObject]。因此,您真正想要的是在这里使用as?执行条件强制转换,并将其放入if let语句中。就像这样

代码语言:javascript
复制
if let myMovers = movers as? [PFObject] {
    // Use myMovers to do what you want 
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32980295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档