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 Methods with Closures

NSJSONObjectWithData Updated with Swift 2

Looks like they have updated the NSJSONObjectWithData after WWDC15 with Swift 2 and this now needs to catch a throw for error handling.

Anyone know the new syntax?

Nothing is updated with Swift 2 yet unless you're running a beta version of Xcode, which you probably shouldn't be doing if you're doing these lessons. Just a heads up.

I’m also looking forward to the answer to this. I’m using Xcode beta with Swift 2. And yes now it suggest to use a try catch.

3 Answers

Will Matthews
Will Matthews
8,127 Points

Looks like the Swift 2 approach is to use the new do-try-catch approach. Here's what worked for me:

    do {
        let jsonDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String: AnyObject]
        completion(jsonDictionary)
    } catch let error {
        print("JSON Serialization failed. Error: \(error)")
    }
Nathan Tallack
Nathan Tallack
22,159 Points

Thank you thank you thank you thank you!!!!!! :)

Hi guys! After poking around, I was able to figure this one out. You just have to add "try!" before NSJSONSerialization. It should look like this.

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

Hope this helps!

Hey man...you saved me from pulling the last bit of hair out of my head! It works!!!!!

I tried both the approach of Will and Zoli, the error got fixed, but when I went ahead with accessing data from web exercise, the label is not updating from web.

when I used the print statement, for both the approach the completion is not getting executed

func downloadJSONFromURL(completion: JSONDictionaryCompletion ){

    let request: NSURLRequest = NSURLRequest(URL: queryURL)

    let dataTask = session.dataTaskWithRequest(request){
        (let data, let response, let error) in


        if let httpResponse = response as? NSHTTPURLResponse{

            switch(httpResponse.statusCode){
            case 200:

                 print("getting positive response +++")

                /*do {

                    print("getting positive response 2+++")

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

                    print("getting positive response 4 ")

                } catch let error {
                    print("JSON Serialization failed. Error: \(error)")
                }*/


                let jsonDictionary = try!NSJSONSerialization.JSONObjectWithData(data!, options:[]) as? [String: AnyObject]
                 print("getting positive response 4 ")
                 completion(jsonDictionary)

            default:
                print("GET request not successful. HTTP status code: \(httpResponse.statusCode)")
            }
        }else{
            print("Error: Not a valid HTTP response")
        }
    }

    dataTask.resume()
}