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 trialAndrew Thompson
Courses Plus Student 1,796 PointsHaving a problem with the newly updated Xcode 6 code in the teacher notes, still not working.
Here is my code:
let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
let forecastURL = NSURL(string: "34.293413,-118.534371", 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()
var urlContents = NSString(contentsOfURL: location, encoding: NSUTF8StringEncoding, error: nil)
println(urlContents)
I get this error: Use of unresolved indentifier 'location'
2 Answers
Chris Shaw
26,676 PointsHi Andrew,
The urlContents
assignment you have isn't needed since you're using sharedSession.downloadTaskWithURL
which collects the result of the request in response
, however to explain why you're getting an error that is because there is no variable called location
in the current scope, that variable exists within the completionHandler
closure therefore it's scoped to that part of the code and can't be used anywhere else.
If that is confusing I'm simply saying you don't need the below.
var urlContents = NSString(contentsOfURL: location, encoding: NSUTF8StringEncoding, error: nil)
println(urlContents)
Andrew Thompson
Courses Plus Student 1,796 PointsOMG Thank you! I was going crazy!!! Haha :)
ch11
3,901 PointsThe code from the tutorial is as follows:
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL!, completionHandler: { (loacation: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
if (error == nil) {
let dataObject = NSData(contentsOfURL: location)
let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary
println(weatherDictionary)
}
})
downloadTask.resume()
}
After deleting the code
let dataObject = NSData(contentsOfURL: location)
then, the following code gives an error: use of unresolved identifier 'dataObject'
let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary
if I get rid of that code, there is no weatherDictionary to use in
println(weatherDictionary)
I'm stuck :(
Andrew Thompson
Courses Plus Student 1,796 PointsAndrew Thompson
Courses Plus Student 1,796 PointsThank you for that, I realized he ended up deleting that, but then still added this code next:
Still giving me an error for 'location' being unresolved identifier.
The video he is clearly typing "location" for contentsofURL, and I've triple checked my code matches 100% to his.
Thank you!
Chris Shaw
26,676 PointsChris Shaw
26,676 PointsAs I said above
location
is scoped only to the closure so you need to execute your code within it, see the below.