【问题标题】:Error creating multiple map annotations from a parse class从解析类创建多个地图注释时出错
【发布时间】:2015-10-06 21:45:01
【问题描述】:

我正在尝试从 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)")
        }
    }

}

提前致谢

【问题讨论】:

  • 请将您的代码直接粘贴到问题中,而不是作为图像,因为这会使回答变得更加困难。

标签: ios swift parse-platform mapkit mapkitannotation


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    相关资源
    最近更新 更多