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 trialDaniel Ferguson
1,850 PointsPlease 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
2,606 PointsThey (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.
Devin Brown
652 PointsAn 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
13,182 PointsThe 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
11,688 PointsYou 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?
Daniel Ferguson
1,850 PointsIt'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
Valeria Bonilla
1,712 PointsI 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
3,679 PointsElijah 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!
Daniel Ferguson
1,850 PointsDaniel Ferguson
1,850 PointsThanks Christopher! Worked 100%
Brian Oei
2,175 PointsBrian Oei
2,175 PointsThis worked for me as well. Thanks
James Crain
7,967 PointsJames Crain
7,967 PointsThanks man, worked for me as well.
Clemens Pfister
10,925 PointsClemens Pfister
10,925 PointsThank you this worked very well!