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

Dino Alves
Dino Alves
3,936 Points

Thread 1 breaking at downloadTask with (lldb) out puting on console

My code is the following:

//
//  ViewController.swift
//  Stormy
//
//  Created by Dino Alves on 2015/01/15.
//  Copyright (c) 2015 Futre. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    private let apiKey = "xxxx"


    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.8267,-122.423", relativeToURL: baseURL)!

        let sharedSession = NSURLSession.sharedSession()

        let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(forecastURL, completionHandler: { (location: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()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

I am running Yosemite with XCode 6.1.1

The Thread 1 keeps breaking at the line where "let downloadTask..." is. The console outputs (lldb) and no weatherDictionary data is displayed.

Please can someone assist with this as I cannot continue with my lesson until I have this fixed. I have read through all questions on this video and no solutions.

2 Answers

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

means you're casting weatherDictionary as an NSDictionary, and promising that it'll get the right type of content from dataObject. You're most probably wrong. Try removing all the downcasting, and see what you're getting from the serialisation:

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

Just ignore the warning, run the app, and check the console. That should help you get closer to what the problem is (a typo in the API key, etc).

I would say try another api key, i just ran your code and it went well the only thing different for me was my api key.