This is my code for making a network call to forecast.io
.
Inside the ViewController
I have:
private let apiKey = ""//my key
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
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
}
})
downloadTask.resume()
}
I'm trying to set my data into an NSDictionary
to be able to access it. I have a bug (green line) which has something to do with weatherDictionary
:
fatal error: unexpectedly found nil while unwrapping an Optional value
I'm unwrapping the dataObject
, so what could be the problem?