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 Using Our JSON Data

use of unresolved identifier 'location.'

Hello fellow Swift coders,

I am currently using XCode 6.1.1. I just finished the Concurrency chapter and I am working it simultaneously in xcode.

I have the following code in my ViewController:

            let dataObject = NSData(contentsOfURL: location)!
            let weatherDictionary: NSDictionary =
                NSJSONSerialization.dataWithJSONObject(dataObject, options:nil, error: nil) as NSDictionary
                println(weatherDictionary)


    }
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
    }

}

I'm getting the error 'use of unresolved identifier 'location'' on the line next to let dataObject. Any ideas on how I can resolve this?

5 Answers

Stone Preston
Stone Preston
42,016 Points

alright the reason you are getting unresolved identifier is because the location only exists within the downloadTaskWithURL closure. you need to put the code inside that closure block:

// Do any additional setup after loading the view, typically from an nib

    let baseURL = NSURL(string:"https://api.forecast.io/forecast/\(apiKey)/")

    let forecastURL = NSURL(string: "25.193896,55.277340", relativeToURL: baseURL)
    let sharedSession = NSURLSession.sharedSession()
    let downloadTask: NSURLSessionDownloadTask =
        sharedSession.downloadTaskWithURL(forecastURL!,
            completionHandler: {( location: NSURL!, response:
                NSURLResponse!, error: NSError!) -> Void in

             //put the code inside here
             let dataObject = NSData(contentsOfURL: location)
             let weatherDictionary: NSDictionary =
                NSJSONSerialization.dataWithJSONObject(dataObject, options:nil, error: nil) as NSDictionary
                println(weatherDictionary)
        })

note: also remove the ! from the end of the NSData initializer call

Stone Preston
Stone Preston
42,016 Points

can you post the code that comes before the line

let dataObject = NSData(contentsOfURL: location)!

Hi Preston,

Before the line, I have the following code:

import UIKit

class ViewController: UIViewController {

private let apiKey = "0101010101010101010101010101"

override func viewDidLoad() {

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from an nib

    let baseURL = NSURL(string:"https://api.forecast.io/forecast/\(apiKey)/")

    let forecastURL = NSURL(string: "25.193896,55.277340", relativeToURL: baseURL)
    let sharedSession = NSURLSession.sharedSession()
    let downloadTask: NSURLSessionDownloadTask =
        sharedSession.downloadTaskWithURL(forecastURL!,
            completionHandler: {( location: NSURL!, response:
                NSURLResponse!, error: NSError!) -> Void in

        })

Awesome! It works now. Thanks Preston

Hey Preston, I am getting an error of

'NSData?' not unwrapped; did you mean to use '!' or '?'?

at the following code

let weatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary

Also I noticed in the video Pasan used 'JSONObjectWithData' while your code has 'dataWithJSONObject'. Are they the same thing? According to apple's documentation, we use JSONObjectWithData to create an Object while dataWithJSONObject to create a data. I am confused why you would use 'dataWithJSONObject'. Thank you for your help, I look forward to your reply!

Here are the rest of my code

let baseURL = NSURL(string: "https://api.forecast.io/forecast/\(apiKey)/")
let forecastURL = NSURL(string: "32.8639051,-117.2223145", relativeToURL: baseURL)

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


           let dataObject = NSData(contentsOfURL: location)
           let weatherDictionary: NSDictionary = 
           NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary
           println(weatherDictionary)

    })