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 Managing Complexity Grand Central Dispatch

peterwu2
peterwu2
3,083 Points

Grand Central Dispatch: Build succeeded, app launched, but then crashed.

I followed along with the video, and Xcode shows no code error. I then ran the build, which succeeded, and the app launched, but then it crashed. The debugger shows the breakpoint is on thread 6 in the following code:

import Foundation

class NetworkOperations {
lazy var config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
lazy var session: NSURLSession = NSURLSession(configuration: self.config)
let queryURL: NSURL

typealias JSONDictionaryCompletion = ([String: AnyObject]?) -> Void

init (url: NSURL) {
    self.queryURL = url
}
func downloadJSONFromURL(completion: JSONDictionaryCompletion) {

    let request: NSURLRequest = NSURLRequest(URL: queryURL)
    let dataTask = session.dataTaskWithRequest(request) {
        (let data, let response, let error) in

        // 1. Check HTTP response for successful GET request

        if let httpResponse = response as? NSHTTPURLResponse {


                // 2. Create JSON object with data
                do {

                    let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: AnyObject]
                    completion(jsonDictionary)
                } catch  {
                    print("GET request not successful. HTTP status code: \(httpResponse.statusCode)")

                }


        } else {
                print("Error:  Not a valid HTTP response")
            }


        }


        dataTask.resume()


    } 
}

Specifically, the breakpoint highlights the code:

let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: AnyObject]

And when I hover my cursor over jsonDictionary, it shows "0 key/value pairs". What should I do from here? Any pointer or direction is greatly appreciated.

P.S. Sorry for the clumsy way of posting the codes blocks. I'm still very new to posting entries on the forum and I tried markdown (like using '''Swift in the code block to format the codes properly) and it didn't seem to work. I'd love to learn to to format my codes better when posting on the forums and I'd appreciate any pointer on that as well.

Thanks!

Peter