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

Getting nil value in console?

Im getting a nil value as an output. Why?

    override func viewDidLoad() {
        super.viewDidLoad()
        let baseURL = NSURL(string:"https://api.forecast.io/forecast/\(apiKey)/")
        let forecastURL = NSURL(string:"43.059329,-70.765962", 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()



    }

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


}
Iulian Popescu
Iulian Popescu
1,363 Points

If the nil value is printed when you make the call means that the call wasn't successfully. Check your error.localizedDescription to see the error you receive and if this doesn't help try to recheck your url.

6 Answers

Jared Watkins
Jared Watkins
10,756 Points

Mark,

I ran your code using my API key, and it worked as expected. Sorry that's not more helpful.

Mark,

Your code looks fine. Can you check the API Key for me.

Tyler Banks
Tyler Banks
3,109 Points

I wrote an if let statement and it's working fine

if let forecastURL = NSURL(string: "Your location", relativeToURL: baseURL) {

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

} }

Jeanne Merle
Jeanne Merle
3,390 Points

Hi,

I tried the if-let statement but it won't work :

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)/")

    if let forecastURL = NSURL(string: "45.165841,5.743491", 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()

    }
}

my apiKey works, the URL also, when I copy-paste it into a browser the data is displayed.

Here is my debugging console : location NSURL! "file:///Users/Jeannette/Library/Developer/CoreSimulator/Devices/3636857E-3428-4413-859C-CE82B184017D/data/Containers/Data/Application/3DD01C61-5158-402C-B865-9EFEE81A8B99/tmp/CFNetworkDownload_bWoyZ4.tmp" 0x00007fc229f61270 response NSURLResponse! 0x00007fc229e21740 0x00007fc229e21740 error NSError! nil None

and the Output : (lldb)

Thx

Tyler Banks
Tyler Banks
3,109 Points

Hi Jeanne, have u tried removing the " ! " symbol in the if let statement after baseURL?

Jeanne Merle
Jeanne Merle
3,390 Points

@Tyler : Yes I have tried with, without, and it is the same.

Tyler Banks
Tyler Banks
3,109 Points

I just tried your code and it works fine, just put in your apiKey between the "" in private let apiKey, compare it with yours, if u don't find any difference just copy and paste it, good luck!

import UIKit

class ViewController: UIViewController {

private let apiKey = ""


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)/")

        if let forecastURL = NSURL(string: "45.165841,5.743491", 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()

        }
    }






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

}

Jeanne Merle
Jeanne Merle
3,390 Points

Thanks Tyler, I have done as you said, and also put my own coordinates in a let "mesCoordonnees" rather than directly into the forecastURL , and it mysteriously worked ! I don't touch anything, it seems to me that it is very fragile ;-)