2016-01-14 43 views
1

因此,我正在設置此應用程序,用戶可以看到他們的位置,並且還能夠看到其他人的位置。對於這個劇本,我正在關注這個問題。 how to retrive location from a PFGeopoint - (Parse.com and Swift) and show it on the map with Xcode 6.2從[PFObject]轉換爲[PFObject]

當我在寫我在這條線得到一個錯誤代碼:

if let proximityArray = objects as? [PFObject] { 

說: '?[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() 

回答

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() 
+0

然後錯誤向下移動If let proximityianray = objects as? [PFObject]並顯示相同的向下錯誤 –

+0

@TomJames請參閱我的答案。它基本上是你不需要鑄造只要你neet解開它 – Ismail

+0

我相信那就是它。我收到一條錯誤信息:「If let position = near ['Where'] as!PFGeopoint」say「用於條件綁定的初始化程序必須具有可選類型,而不是PFGeopoint。可能與該對象沒有任何關係? –