【问题标题】:NSURL problems in SwiftSwift 中的 NSURL 问题
【发布时间】:2015-12-25 06:12:07
【问题描述】:

我正在构建一个天气应用程序,而这整个代码块是一个错误。我几乎尝试了所有方法。

这是代码:

func getCurrentWeatherData() -> Void {
    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
    let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)

    let sharedSession = NSURLSession.sharedSession()
    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 = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) 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
        })
      } else {
        let networkIssueController = UIAlertController(title: "Error", message: "Unable to load data. Connectivity error!", preferredStyle: .Alert)
        let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
        networkIssueController.addAction(okButton)

        let cancelButton = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
        networkIssueController.addAction(cancelButton)

        self.presentViewController(networkIssueController, animated: true, completion: nil)

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
          self.refreshActivityIndicator.stopAnimating()
          self.refreshActivityIndicator.hidden = true
          self.refreshButton.hidden = false
        })
      }
    })
}

这是错误:

"/Users/Ethan/Downloads/Weather-master/Weather/ViewController.swift:39:117: '(NSURL!, NSURLResponse!, NSError!) -> Void' 不能转换为 '(NSURL?, NSURLResponse ?, NSError?) -> Void'"

编辑

解决上面的错误...现在 代码是: 让 weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary

错误是

/Users/Ethan/Downloads/Weather-master/Weather/ViewController.swift:42:85:调用中的额外参数“错误”

【问题讨论】:

  • location: NSURL!, response: NSURLResponse!, error: NSError! 改为location: NSURL?, response: NSURLResponse?, error: NSError?
  • 您使用的是 Xcode 7 / Swift 2 吗?如果是,则使用do - catch 实施新的错误处理。并更改标签。
  • 不要完全改变你的问题。如果您有新问题,请提出新问题。

标签: ios swift nsurl


【解决方案1】:

改变

   location: NSURL!, response: NSURLResponse!, error: NSError!  

   location: NSURL?, response: NSURLResponse?, error: NSError?  

如果你使用的是 swift2

   do {
        let jsonResult = try NSJSONSerialization.JSONObjectWithData(dataObject, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
   } catch let error as NSError {
         print(error)
   }

【讨论】:

  • 现在让weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary
  • 错误是 /Users/Ethan/Downloads/Weather-master/Weather/ViewController.swift:42:85: Extra argument 'error' in call
  • 效果很好,但我收到了另一个错误,这是代码
  • let downloadTask = try NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL?, response: NSURLResponse?, error: NSError?) -> 错误中的无效是无法分配给'NSURLSessionDownloadTask.Type' 类型的不可变表达式
猜你喜欢
  • 2019-03-11
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
相关资源
最近更新 更多