ios - 从解析类创建多个 map 注释时出错

标签 ios swift parse-platform mapkit mapkitannotation

我尝试从 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/

相关文章:

ios - 在 objective-c 中有效解析单元素 JSON

swift - UIControl 添加目标(_ :action:for:) Method in Swift 3. 2

ios - 在 Swift 的 MailJet 电子邮件中附加 PDF

ios - 解析通知问题。 iOS 9.1、Xcode 7.1.1

php - 尝试使用 PHP Parse.com 查询日期之间

iphone - Amazon S3 在 PUT 上出现 501 错误

ios - 未在 swift 类中调用 AsyncSocket 回调

ios - CoreData链表?

swift 3/macOS : Open window on certain screen

javascript - ExtJS:使用远程加载的单例值进行存储定义