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 Refactoring Using Codable Introducing the Codable Protocol

Jialong Zhang
Jialong Zhang
9,821 Points

App doesn't work with Codable Protocol

I added Codeable to Weather and CurrentWeather class. My app doesn't work and no errors were shown in the console.

Below is the DarkSkyAPICilent class

import Foundation

class DarkSkyAPIClient{

    fileprivate let darkSkyApiKey = "<API_KEY>"
    lazy var baseUrl : URL = {
        return  URL(string: "https://api.darksky.net/forecast/\(darkSkyApiKey)/")!
    }()
    let decoder = JSONDecoder()


    let session: URLSession
    init(configuration: URLSessionConfiguration){
        self.session = URLSession(configuration: configuration)
    }
    convenience init(){
        self.init(configuration: .default)
    }



    typealias WeatherCompletionHandler = (Weather?, Error?) -> Void
    typealias CurrentWeatherCompletionHandler = (CurrentWeather?, Error?) -> Void


    private func getWeather(at coordinate: Coordinate, completionHandler completion: @escaping WeatherCompletionHandler){
        guard let url = URL(string: coordinate.description, relativeTo: baseUrl)else{
            completion(nil, DarkSkyError.invalidUrl)
            return
        }

        let request = URLRequest(url: url)

        let task = session.dataTask(with: request) {data, response ,error in
            if let data = data {
                guard let httpResponse = response as? HTTPURLResponse else{
                    completion(nil, DarkSkyError.requestFailed)
                    return
                }
                if httpResponse.statusCode == 200{
                    do{
                        let weather = try self.decoder.decode(Weather.self, from: data)
                        completion(weather, nil)
                    }catch let error{
                        completion(nil, error)
                    }
                } else{
                    completion(nil, DarkSkyError.invalidData)
                }


            }else if let error = error{
                completion(nil, error)
            }

        }

         task.resume()
    }


    func getCurrentWeahter(at coordinate: Coordinate, completionHandler completion: @escaping CurrentWeatherCompletionHandler){
        getWeather(at: coordinate) {weather, error in
            completion(weather?.currently, error)
        }
    }



}//end DarkSkyAPICilent
Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

What do you mean by "doesn't work"? Is that data no showing?

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

Edit note: I removed the API key, as that is sensitive information and you don't want anyone taking it. In this case it shouldn't be an issue, but you should always make sure you remove those from code snippets in the future.

3 Answers

Cory Cromer
Cory Cromer
5,171 Points

If you copied and pasted the TH project into your file then the problem is not with the DarkSkyAPIClient file its somewhere else. What I would suggest is to reimplement the print(currentWeather) and print(error) statements in your viewDidLoad class in your ViewController file. See below.

 override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        client.getCurrentWeather(at: Coordinate.myHouse) { [unowned self] currentWeather, error in
            print(currentWeather)
            print(error)
            if let currentWeather = currentWeather {
                let viewModel = CurrentWeatherViewModel(model: currentWeather)
                self.displayWeather(using: viewModel)
            }
        }  
    }

Then read the error statement in the console. If I had to guess, you have a decoder error. Make sure that your properties in CurrentWeather class are spelled the same as the DarkSky documentation or the decoder won't be able to recognize them.
Hope this helps.

Jialong Zhang
Jialong Zhang
9,821 Points

yes, my data doesn't change or response with Codable protocol.

Jialong Zhang
Jialong Zhang
9,821 Points

Thanks. I downloaded the project under the video, which is working fine. For my own version, the app doesn't populate the data after I implemented Codable protocol. It is weird.

Caleb Kleveter
Caleb Kleveter
Treehouse Moderator 37,862 Points

The snippet you posted looks fine, so I would suggest looking for differences between the TH project and your project. Hope you find the issue!

Deysha Rivera
Deysha Rivera
5,088 Points

I had the same issue, and downloaded the TH project, which works just fine. I combed over the code line by line and did not see any differences. I then copied and pasted from TH into my project, and it still does not work. The view controller does not update, and I get frame loading errors.