从[PFObject]向[PFObject]

时间:2016-01-14 15:51:36

标签: ios swift object geolocation

所以我正在设置这个应用程序,用户可以看到他们的位置,也能够看到其他人的位置。对于脚本我正在关注这个问题。 how to retrive location from a PFGeopoint - (Parse.com and Swift) and show it on the map with Xcode 6.2

当我编写代码时,我在这一行得到了一个错误:

  if let proximityArray = objects as? [PFObject] {

话说:

  

DownCast来自'[PFObject]?'到'[PFObject]'只展开选项:   你的意思是使用'!'?'

所以我试着添加:

 for object in objects as! [PFObject]{

我仍然得到同样的错误。

老实说,我不知道对象是什么。

我不知道如何解决这个问题,如果对解决问题有任何帮助,我将不胜感激。

谢谢

对于那些想要其余代码的人来说,这里是:

func filterByProximity() {
            PFQuery(className: "location")
                .whereKey("where", nearGeoPoint: myGeoPoint, withinKilometers: 5000.0)     //(474)
                .findObjectsInBackgroundWithBlock ({
                    objects, error in

                    for object in objects as! [PFObject]{

                    if let proximityArray = objects as? [PFObject] {

                        print("****** here the proximity matches: \(proximityArray)")
                        for near in proximityArray {
                            println("here they are \(near)")
                            if let position = near["where"] as! PFGeoPoint {
                                let theirLat = position.latituide
                                let theirLon = position.longitude
                            }

                            let theirLat = near["where"].latitude as Double
                            let theirlong = near["where"].longitude as Double
                            let location = CLLocationCoordinate2DMake(theirLat, theirlong)
                            let span = MKCoordinateSpanMake(0.05, 0.05)
                            let region = MKCoordinateRegionMake(location, span)
                            self.MapView?.setRegion(region, animated: true)
                            let theirAnotation = MKPointAnnotation()
                            theirAnotation.setCoordinate(location)
                            self.mapView.addAnnotation(anotation)

                        }
                    }
                        }
                })
        }

        filterByProximity()

1 个答案:

答案 0 :(得分:0)

请将您的代码更改为以下代码:

func filterByProximity() {
        PFQuery(className: "location")
            .whereKey("where", nearGeoPoint: myGeoPoint, withinKilometers: 5000.0)     //(474)
            .findObjectsInBackgroundWithBlock ({
                objects, error in

                if let objects = objects {
                   for object in objects{

                 let proximityArray = objects 

                    print("****** here the proximity matches: \(proximityArray)")
                    for near in proximityArray {
                        println("here they are \(near)")
                        if let position = near["where"] as? PFGeoPoint {
                            let theirLat = position.latituide
                            let theirLon = position.longitude
                        }

                        let theirLat = near["where"].latitude as Double
                        let theirlong = near["where"].longitude as Double
                        let location = CLLocationCoordinate2DMake(theirLat, theirlong)
                        let span = MKCoordinateSpanMake(0.05, 0.05)
                        let region = MKCoordinateRegionMake(location, span)
                        self.MapView?.setRegion(region, animated: true)
                        let theirAnotation = MKPointAnnotation()
                        theirAnotation.setCoordinate(location)
                        self.mapView.addAnnotation(anotation)
                     }
                 }
            })
    }

    filterByProximity()

这应该摆脱选项错误。但我认为你的代码中存在逻辑错误,它应该是这样的:

func filterByProximity() {
    PFQuery(className: "location")
        .whereKey("where", nearGeoPoint: myGeoPoint, withinKilometers: 5000.0)     //(474)
        .findObjectsInBackgroundWithBlock ({
            objects, error in

            if let proximityArray = objects {

                print("****** here the proximity matches: \(proximityArray)")
                for near in proximityArray {
                    println("here they are \(near)")
                    if let position = near["where"] as? PFGeoPoint {
                        let theirLat = position.latituide
                        let theirLon = position.longitude
                    }

                    let theirLat = near["where"].latitude as Double
                    let theirlong = near["where"].longitude as Double
                    let location = CLLocationCoordinate2DMake(theirLat, theirlong)
                    let span = MKCoordinateSpanMake(0.05, 0.05)
                    let region = MKCoordinateRegionMake(location, span)
                    self.MapView?.setRegion(region, animated: true)
                    let theirAnotation = MKPointAnnotation()
                    theirAnotation.setCoordinate(location)
                    self.mapView.addAnnotation(anotation)
                }
            }
        })
}

filterByProximity()