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

Please help with coding error/issue

I am having issues with the:

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

part of this course. I am running Yosemite, with xCode 6.1 and it wants me to change:

forecastURL to forcastURL!

But that too is giving me runtime errors

5 Answers

Christopher St. George
Christopher St. George
2,606 Points

They (Apple) may have recently changed the swift NSURL initializers, as I read somewhere recently Swift is still an 'infant' and so classes and methods are more likely to be tweaked in Swift as its developers refine it.

The way that I fixed this was to make an 'if let' statement:

if let forecastURL = NSURL(string: "___enter your URL suffix here___", 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()
}

In general, writing your code this way rather than forcing it to unwrap an optional is the 'safer' way to write it... best that it deals with errors gracefully and can inform the user rather than crashing.

Thanks Christopher! Worked 100%

This worked for me as well. Thanks

James Crain
James Crain
7,967 Points

Thanks man, worked for me as well.

Clemens Pfister
Clemens Pfister
10,925 Points

Thank you this worked very well!

An exclamation mark was required for me to get this block of code to work. Specifically for forecastURL!.

Here's what I used:

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

The issue I realized I had is that I had to make sure the values of NSURL! , NSURLResponse!, and NSError! weren't in blue boxes by confirming that they were the proper values.

Here's the entire block:

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

downloadTask.resume()
Jason Wayne
Jason Wayne
11,688 Points

You should change it to forecastURL!, if you actually type out the code, the forecast URL is actually of type NSURL? instead of NSURL. Hence, in order to use the value, you have to unwrap it.

In regards to the error, what error were you getting?

It's a runtime issue, not a startup issue. I replaced both the NSURL (added the ?) and the forecastURL (added the !) and I'm still having the same issue.

I've linked photos of my issues

https://www.dropbox.com/s/fnf1pkkk4bqpoe4/Screen%20Shot%202014-11-18%20at%2010.15.13%20pm.png?dl=0

https://www.dropbox.com/s/s2wqaykdie0sc4n/Screen%20Shot%202014-11-18%20at%2010.15.00%20pm.png?dl=0

I used a mix of what christopher said and used the ? mark on NSURLResponse and it wokred

Code should look like this:

if let forecastURL = NSURL(string: "enter your URL suffix here", 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()

Jason Meinzer
Jason Meinzer
3,679 Points

Elijah Gartin's response is what fixed it for me - NSURL! and NSURLResponse! were in blue boxes and all you have to do is highlight the blue boxes and hit enter. Voila. No error. Thanks!