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) Concurrency Writing Concurrent Networking Code

println(repsonse) is Nil. How to get NSData(contentsOfURL : forecastURL!) to work

I don't think the code is wrong because it compiles successfully, but there is no data inside of the URL according to the console.

println(response)  //displays "nil"
    private let apiKey = "apikey"


        let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")

        let forecastURL = NSURL(string: "40.724245,-73.562374", relativeToURL: baseURL)

        let sharedSession = NSURLSession.sharedSession()

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

                println(response)

            })
        downloadTask.resume()

Please help. Thank you

3 Answers

Robert Bojor
PLUS
Robert Bojor
Courses Plus Student 29,439 Points

A good practice is to also output the error that comes back into that completion handler. This way you don't have to wait until a connection will go through and you receive some actual data.

By checking the error message if it's nil or not will also give you the opportunity to inform the user that something went wrong and they have to refresh or try fetching the data again.

Waited 1hr and magically it worked.

alahab2
alahab2
9,709 Points

I am having the same issue... Let's see if waiting a few hours help me too...