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

Mads Thorsen
Mads Thorsen
3,464 Points

Getting a .jsonParsingFailure

I can't see why i get a .jsonParsingFailure when i run my build?

class DarkSkyAPIClient { fileprivate let apiKey = xxxxxxxxxxxxxxxxxxxxxx lazy var baseUrl: URL = { return URL(string: "https://api.darksky.net/forecast/\(self.apiKey)/")! } ()

let downloader = 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 {
        completion(nil, .invalidUrl)
        return
    }

    let request = URLRequest(url: url)
    let task = downloader.jsonTask(with: request) { json, error in

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

        completion(currentWeather,nil)
    }

    task.resume()
}

}

Austin Jones
Austin Jones
8,305 Points

You want to make sure you are returning the correct strings that match the "currently" dictionary from the API. Make sure the struct "Key" in CurrentWeather.swift looks like this.

    struct Key {
        static let temperature = "temperature"
        static let humidity = "humidity"
        static let precipProbability = "precipProbability"
        static let summary = "summary"
        static let icon = "icon"
    }

those strings must be precise since it is matching up with the data from the API. I misspelt the "precipProbability" string as "precipProbabability" and got the same error back.