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

Does anyone have a working example of the finished code for this video using Xcode 6.3.1?

I made the changes suggested in the teachers notes but am still getting errors.

This is the code I have now

import UIKit

class ViewController: UIViewController {

    private let apiKey = "96c01b927564025ef6c5d41ad0663aee"

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

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

        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()
    }

this gives an error: use of unresolved identifier 'location' another forum suggestion was to remove the line where the error occurred, stating it's redundancy.

            let dataObject = NSData(contentsOfURL: location)

However, then the following line gives an error: use of unresolved identifier 'dataObject'

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

It would be very helpful to be able to see the correct code for comparison. THANKS in advance!!!

Anthony Munoz
Anthony Munoz
1,935 Points

location should be unwrapped let dataObject = NSData(contentsOfURL: location!)

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

Thanks for the suggestion Anthony! :)

I added the bang to unwrap location and was still getting the error. Then I looked up into the earlier code, to see where 'location' was being referenced from, and found that I had misspelled it as 'loacation' . After fixing that typo the code seems to work with or without unwrapping location. Is it still necessary to unwrap location?

Thanks again for responding to my question!!!

Do you have any insight into this other question about generating random numbers?

1 Answer

Anthony Munoz
Anthony Munoz
1,935 Points

Glad you got it working! Does the data look right? I had to unwrap, I got error saying NSData was unwrapped. I'm using Version 6.3.1 (6D1002)