本文介绍了Swift 中的 NSURL 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个天气应用程序,这整个代码块是一个错误.我几乎什么都试过了.

I'm building a weather app and this whole block of code is an error. I've tried almost everything.

这是代码:

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

Solve error above one...now theCode is:let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary

错误是

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

推荐答案

change

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

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

如果您使用的是 swift2

if you are using swift2

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

这篇关于Swift 中的 NSURL 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 20:52