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 trial

iOS Build a Weather App Dark Sky API Client Cleaning Up The View Controller

Christian A. Castro
Christian A. Castro
30,501 Points

Error 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)

        }
    }
}
Jhoan Arango
Jhoan Arango
14,575 Points

Hello,

Have you checked the URL that you are constructing ? it may be wrong

Christian A. Castro
Christian A. Castro
30,501 Points

Jhoan Arango Thank you for your quick response. I actually did!!! but I will double check again, I will keep you posted, blessing!

2 Answers

Hey 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
Christian A. Castro
30,501 Points

Joseph 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!!