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

Add a ! to forecastURL

I was having trouble with code as it was written in the tutorial. May be an issue as a result of new release of xcode.

Anyway, if you're having trouble try unwrapping the forecastURL optional (see below).

''' override func viewDidLoad() { super.viewDidLoad()

    let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
    let forecastURL = NSURL(string: "37.8267,-122.423", relativeToURL: baseURL)
    let sharedSession = NSURLSession.sharedSession()
    let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
        println(location)
        println(response)
    })
    downloadTask.resume()
}

'''

Note - You need to state apiKey beforehand

5 Answers

Jason Wayne
Jason Wayne
11,688 Points

One thing I notice that is the reason why we needed to add the bang symbol (!) is because the forecastURL created is actually an optional NSURL string. It's different between a regular NSURL, since it can accept having it to be nil. So, when you type out NSURL(string:...............), Xcode will give a suggested list, and beside the NSURL, it is stated that it is an optional NSURL.

Hope that makes sense. =S

Tom Davis
Tom Davis
1,796 Points

This solved my problem as well. They need to update this in the notes.

Brett Dillworth
Brett Dillworth
4,170 Points

Thanks! I was stuck on that for a while.

Just to add, make sure your device is connected to wifi as well!!

My device's connection was dropped during the test and I kept getting 'nil'. After resetting my phone's wifi setting, everything works again.

Hey guys, i have the same problem with the optional String but it doesn't help to add the bang. What could be the problem?

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

    downloadTask.resume()

Thanks!

Greg Melson
Greg Melson
3,037 Points

change this piece in your code : "println(location) println(response) " to "println(response); println(error)"

I think your missing a semi colon

So it will look like this: "let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forcastURL!, completionHandler: { (location: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in println(location) ; println(response) }) downloadTask.resume()"

Hope that helps