I am working on weather API. getting error in following code:
fileprivate let openWeatherMapBaseURL = "http://api.openweathermap.org/data/2.5/weather"
fileprivate let openWeatherMapAPIKey = "b7ac98fd9b59acbe6078468d865bd908"
func getWeather(_ city: String) {
// This is a pretty simple networking task, so the shared session will do.
let session = URLSession.shared
let weatherRequestURL = URL(string:"http://api.openweathermap.org/data/2.5/weather?q=\(city)&APPID=\(openWeatherMapAPIKey)")!
let dataTask = session.dataTask(with: weatherRequestURL, completionHandler: {
(data: Data?, response: URLResponse?, error: NSError?) in
if let error = error{
print("Error:\n\(error)")
}
else{
print("Raw data:\n\(data!)\n")
let dataString = String(data: data!, encoding: String.Encoding.utf8)
print("Human-readable data:\n\(dataString!)")
}
} as! (Data?, URLResponse?, Error?) -> Void)
dataTask.resume()
}}
Getting Error in this line:
let dataTask = session.dataTask(with: weatherRequestURL, completionHandler: {
error:
unexpectedly found nil while unwrapping an Optional value
do anyone know what is the solution for this?