2015-12-21 75 views
0

我需要在dataTaskWithRequest的完成塊內部執行一些代碼。這段代碼必須等待網絡抓取完成,然後構建返回UIView的UI。問題是我得到關於在void函數中返回值的編譯器錯誤。如何從完成塊返回值?

代碼看起來像這樣

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

    var rating:Double? 

    var id = Int(marker.userData["orgId"] as! String) 

    let request = NSMutableURLRequest(URL: NSURL(string: urlToFetchFrom)!) 
    request.HTTPMethod = "POST" 
    let postString = "org_id=\(id!)" 
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) 

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

     data, response, error in 

     var json = JSON(data: data!) 

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

     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 

     if rating != 0 { 

      self.window?.stars.image = UIImage(named: "\(self.showStars(rating!))") 
      self.window?.rating.text = String(rating!) 
     } 


     return self.window 

    } 

    task.resume()   

} 

如何修改代碼,以便它不給我的編譯器錯誤?

+0

爲什麼你需要返回窗口? –

回答

0

您不能從void關閉返回,而不是返回,因爲完成處理程序可能會在當前執行離開函數後的某個時間點在不久的將來運行。爲什麼你不能在完成處理程序中調用resume()函數?