2015-12-09 28 views
0

在我的應用程序用戶點擊標記,我需要調用Web服務才能顯示信息。我使用NSURLSession.dataTaskWithRequest,然後調用主隊列上的dispatch_async以向用戶顯示標記。但是,我得到一個錯誤試圖通過參數在dispatch_asyncdataTaskWithRequest之後的Dispatch_async

的代碼如下

func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView! { 

    let request = NSMutableURLRequest(URL: NSURL(string: "http://78.27.190.58:3300/api/get_location_ratings")!) 
    request.HTTPMethod = "POST" 
    let postString = "org_id=\(marker.userData["orgId"] as! Int)" 
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) 

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { 

     data, response, error in 

     var json = JSON(data: data!) 

     var rating = json["results"]["avg_stars"].doubleValue 

     dispatch_async(dispatch_get_main_queue()) { (mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!) -> UIView! in 

      self.window = NSBundle.mainBundle().loadNibNamed("infoWindow", owner: self, options: nil).first! as! infoWindow 

      self.window?.name.text = marker.title 
      self.window?.adress.text = marker.snippet 
      self.window?.stars.image = UIImage(named: "\(showStars((marker.userData["Rating"] as! NSString).doubleValue))") 
      self.window?.rating.text = String((marker.userData["Rating"] as! NSString).doubleValue) 

      return window 


     } 

    } 

    task.resume() 
} 

的錯誤是

enter image description here

我在做什麼錯?

+0

檢查我的新的答案。我以前的答案是錯誤的。 –

回答

0

在您的dispatch_async中,您使用了一個帶有兩個參數(mapView和)的閉包,但沒有向這些參數傳遞任何東西。

+0

如何做到這一點? –

+0

@vien給了你答案。你已經有了這些函數的變量,你根本不需要它們。 – Tim

+0

不能這樣工作。抱怨「void函數中出現意外的非空值返回值」 –

0

因爲您使用的是dataTaskWithRequest,所以不能直接返回值。所以你必須在這種情況下使用閉包。您可以更改代碼如下:

func mapView(mapView: GMSMapView!, markerInfoWindow marker: GMSMarker!, completion:(UIView!) -> Void) { 

    let request = NSMutableURLRequest(URL: NSURL(string: "http://78.27.190.58:3300/api/get_location_ratings")!) 
    request.HTTPMethod = "POST" 
    let postString = "org_id=\(marker.userData["orgId"] as! Int)" 
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) 

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { 

     data, response, error in 

     var json = JSON(data: data!) 

     var rating = json["results"]["avg_stars"].doubleValue 

     dispatch_async(dispatch_get_main_queue()) { 

      let view = UIView(frame: self.view.frame) 

      self.window = NSBundle.mainBundle().loadNibNamed("infoWindow", owner: self, options: nil).first! as! infoWindow 

      self.window?.name.text = marker.title 
      self.window?.adress.text = marker.snippet 
      self.window?.stars.image = UIImage(named: "\(showStars((marker.userData["Rating"] as! NSString).doubleValue))") 
      self.window?.rating.text = String((marker.userData["Rating"] as! NSString).doubleValue) 

      completion(window) 


     } 

    } 

    task.resume() 
} 

並使用它:

your_instace.mapView(mapview, markerInfoWindow: GMSMakre) { (view: UIView!) -> Void in 
     //do somethinghere 
    }