Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialChristian A. Castro
30,501 PointsError message, Nil Optional(Stormy.DarkSkyError.invalidUrl) Build a Weather APP! HELP PLEASE !??
Any idea why I'm unable to retrieve the information from the Darksky API.. Error message that prints on the console: Error message, Nil Optional(Stormy.DarkSkyError.invalidUrl)
This is my DarkAPIClient class:
import Foundation
class DarkSkyAPIClient
{
fileprivate let darkSkyApiKey = "a43da7b64e8d9a6e918a4ef22e56f6a8"
lazy var baseUrl: URL = {
return URL(string: "https://api.darksky.net/forecast/\(self.darkSkyApiKey)/")!
}()
let downloader = JSONDownloader()
typealias WeatherCompletionHandler = (Weather?, DarkSkyError?) -> Void
typealias CurrentWeatherCompletionHandler = (CurrentWeather?, DarkSkyError?) -> Void
private func getWeather(at coordinate: Coordinate, completionHandler completion: @escaping WeatherCompletionHandler)
{
guard let url = URL(string: coordinate.description, relativeTo: baseUrl) else
{
completion(nil, .invalidUrl)
return
}
let request = URLRequest(url: url)
let task = downloader.jsonTask(with: request) {json, error in
guard let json = json else
{
//If is nil..
completion(nil, error)
return
}
guard let weather = Weather(json: json) else
{
completion(nil, .jsonParsingFailure)
//Exit the current scope
return
}
completion(weather, nil)
}
task.resume()
}
func getCurrentWeather(at coordinate: Coordinate, completionHandler completion: @escaping CurrentWeatherCompletionHandler)
{
//Calling
getWeather(at: coordinate){weather, error in
completion(weather?.currently, error)
}
}
}
Christian A. Castro
30,501 PointsJhoan Arango Thank you for your quick response. I actually did!!! but I will double check again, I will keep you posted, blessing!
2 Answers
Joseph Devlin
iOS Development with Swift Techdegree Student 10,403 PointsHey not sure if you found a solution yet, however I was having the same problem and I think I found the cause. Check your coordinate file. I had a space between latitude and longitude making my url invaild.
extension Coordinate: CustomStringConvertible { var description: String { return"(latitude),(longitude)" }
Christian A. Castro
30,501 PointsJoseph Devlin thank you for your feedback!! I actually was missing some extensions within the CurrentWeather swift file. Once I added that code and changed certain variables, it just started working!! Have a bless one!!
Jhoan Arango
14,575 PointsJhoan Arango
14,575 PointsHello,
Have you checked the URL that you are constructing ? it may be wrong