【问题标题】:NSURL and NSURLResponse syntaxNSURL 和 NSURLResponse 语法
【发布时间】:2015-12-27 05:17:00
【问题描述】:

do catch 不工作,这是错误:

从'(NSURL?, NSURLResponse?, NSError?) throws -> Void' 类型的抛出函数到非抛出函数类型'(NSURL?, NSURLResponse?, NSError?) -> Void'的无效转换

这是代码:

let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL?, response: NSURLResponse?, error: NSError?) -> Void in

如果有帮助,这就是全部代码:

let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in       
  if (error == nil){
    let dataObject = NSData(contentsOfURL: location!)
    let weatherDictionary : NSDictionary = try NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: NSError?) as! NSDictionary
    let currentWeather = Weather(weatherDictionary: weatherDictionary)
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
      self.currentTemperature.text = "\(currentWeather.temperature)"
      self.iconView.image = currentWeather.icon!

      let formatter = NSDateFormatter()
      formatter.timeStyle = .ShortStyle
      self.currentTime.text = formatter.stringFromDate(NSDate())
      self.humidity.text = "\(Int(currentWeather.humidity * 100))%"
      self.rain.text = "\(Int(currentWeather.precipProbability))%"
      self.summary.text = "\(currentWeather.summary)"

      self.refreshActivityIndicator.stopAnimating()
      self.refreshActivityIndicator.hidden = true
      self.refreshButton.hidden = false
        })

【问题讨论】:

  • 你找到解决这个问题的方法了吗?任何帮助将不胜感激,因为我遇到了完全相同的问题。

标签: swift2 nsurl nsurlsession


【解决方案1】:

尝试从调用中删除倒数第二个错误参数。然后使用 do catch 块来处理错误。

sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL?, response: NSURLResponse?) -> Void in

【讨论】:

  • 那没用,它让我放一个感叹号,所以我做了,然后它又回到了原来的错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-24
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
  • 2011-07-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多