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 with Swift (Retired) Data Modeling With Structures Cleaning Up Our Date

Eliot Murton
Eliot Murton
7,315 Points

No code error notifications. Yet, output is not the same as in the video. Why?

I have followed the videos and my code has no error notifications. Yet, 40 seconds into the video, when we are asked ti run the programme and println the temperature I get a discrepancy. Rather than the output simply being a number, I get this:

<NSHTTPURLResponse: 0x7af95f50> { URL: https://api.forecast.io/forecast/6f3bd180cccb667a7f39dfbb00e9a8e7/-42.785829,147.061822 } { status code: 200, headers {
    "Cache-Control" = "max-age=854";
    Connection = close;
    "Content-Encoding" = gzip;
    "Content-Type" = "application/json; charset=utf-8";
    Date = "Fri, 13 Feb 2015 14:45:46 GMT";
    Etag = "\"-1001087721\"";
    Expires = "Fri, 13 Feb 2015 15:00:00 +0000";
    Server = "nginx/1.1.19";
    "Transfer-Encoding" = Identity;
    "X-Forecast-API-Calls" = 3;
    "X-Hostname" = "cowrie-shell";
    "X-Powered-By" = Express;
    "X-Response-Time" = 98ms;
} }
60

Why?

Eliot Murton
Eliot Murton
7,315 Points

The "60" must be the output I am looking for but I dont want it with all the other infom. Here is the code from the ViewController that got me here:

import UIKit

class ViewController: UIViewController {

    private let apiKey = "6f3bd180cccb667a7f39dfbb00e9a8e7"

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
        let forecastURL = NSURL ( string: "-42.785829,147.061822", relativeToURL: baseURL)

        let sharedSession = NSURLSession.sharedSession()
        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in println(response)

            if (error == nil) {/*This makes error easier to understand if it occures*/
            let dataObject = NSData(contentsOfURL: location)
            let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject!, options: nil, error: nil) as NSDictionary

            let currentWeather = Current(weatherDictionary: weatherDictionary)
            println(currentWeather.temperature)

            }

        })
        downloadTask.resume()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

1 Answer

You still have the println(response) by your void in the let downloadTask:NSURLSessionDownloadTask