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 Refreshing the Weather

Ingo Ngoyama
Ingo Ngoyama
4,882 Points

I am getting nil Optional(Stormy.DarkSkyError.invalidUrl)

This is my DarkSkyAPI code that calls that error:<<<<<<<<<<<<

import Foundation

class DarkSkyAPIClient { fileprivate let apiKey = "4bcc4b5d0feab3ff87823f5a2c6b7387" // store the API key (paste from VC) and call it ApiKey

// base url to interact with the DkSky api used to make each call when we need it in future
lazy var baseUrl: URL = {

    //paste "base" from VC.
    //let base = The Darksky connection URL but mmodified as below
    //chg "let base=" to  "return" and "darksky" to "self.apiKey"
    //now we have an optional Url but we force unwrap with !
    return URL(string: "https://api.darksky.net/forecast/\(self.apiKey)/")! // its cool to  crash with ! if url cant be created.
}()

let downloader = JSONDownloader() // hold on to an  instance of JSONDownloader()

typealias CurrentWeatherCompletionHandler = (CurrentWeather?, DarkSkyError?)-> Void

func getCurrentWeather(at coordinate: Coordinate, completionHandler completion: @escaping CurrentWeatherCompletionHandler) {


    guard let url = URL(string: coordinate.description, relativeTo: baseUrl) else {// make url
        completion(nil, .invalidUrl)// make this error in DarkSkyError file
        return
    }

    let request = URLRequest(url: url)//make a requestor to use to make requests with

    // ask JSONDownloader to make a request on our behalf
    let task = downloader.jsonTask(with: request) { json, error in

        // after we create the Current Weather instance and convert it to Json in the CurrentWeather file
        guard let json = json else{// unwrap the task
            completion(nil, error)
            return
        }// now we have a valid json object

        guard let currentWeatherJson = json["currently"] as? [String: AnyObject], let currentWeather = CurrentWeather(json: currentWeatherJson) else {
            completion(nil, .jsonParsingFailure)
            return
        }

        completion(currentWeather,nil)

        // now the taks is made and ready
    }

    task.resume() // this will actually start the task
}

}

Max Ramirez
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Max Ramirez
iOS Development Techdegree Graduate 17,361 Points

Ingo, I am having the same issue, and I have viewed this code, and looked for every possible detail including spacing. What could it possibly be? And why does this happen in the first place?

1 Answer

Ingo Ngoyama
Ingo Ngoyama
4,882 Points

I talked to my LA SwifterCoders network on Slack and narrowed it down to the coordinates where I saw that I put space after the latitude in my coordinate description.