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

Problem with NSJSONSerialization returning nil

I am having a problem with a NSJSONSerialization object. When I run the application it crashes with a Fatal error. Nil found when unwrapping optional value. What could cause this? I am using a commercial web service, so I feel confident that the JSON has the correct structure. Until it hits the NSJSONSerialization class I can println and get a valid JSON object.

Here is the code block that is throwing the exception

let sessionConnection = NSURLSession.sharedSession()

    let dataDownloadTask: NSURLSessionDownloadTask = sessionConnection.downloadTaskWithURL(dataSetAuthorizedURL!, completionHandler: { (dataLocation: NSURL!, dataResponse: NSURLResponse!, error: NSError!) -> Void in

    if (error == nil) {

        let dataObject = NSData(contentsOfURL: dataLocation)

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

        println(dataDictionary)
        }

    })
    dataDownloadTask.resume()

At the dataDictionary constant it crashes and generates the found nil error. If I don't downcast to an NSDictionary and keep it as AnyObject? it just prints a nil value.

If I print the dataObject it prints a 5mb file of what looks like serialized data.

Thanks in advance.

2 Answers

Hey Keith, The problem is that somewhere a variable is returning nil. If you say that if you don't downcast it doesn't return an error but just prints nil it looks like your problem is going to be where you serialise your data.

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

My best guess is that 'dataDictionary' is returning nil which means that your serialisation is failing so therefore 'dataObject!' is nil and doesn't hold a value.

May I ask why you are unwrapping 'dataObject'? as it's not an optional value from what I can see. That shouldn't cause an error though.

What's the difference between dataLocation & dataResponse? If you're following the weather app and using the api, I believe that you should be using dataResponse.

Other then that, it looks like you are serialising nothing so therefore your dataDictionary is returning nil.

Hope you get it sorted, Alex

Thanks, I will try dataResponse and see how that goes. I agree that something is going wrong at the dataObject var and an empty variable is being passed to the NSJSONSerialization. Just not sure what is happening.

Honestly, I am unwrapping the dataObject because my version of xCode (6.1.1) keeps prompting for an unwrap. It's been doing that since I did the weather app project. I had to unwrap variables that the instructor did not, or the compile would crash. Not sure what is causing that.

This problem is happening in an attempt to deploy what I learned from completing the weather app. I am trying to create an app that uses UITableView to iterate through data in a service, then apply methods to the data. It's a good problem. Solving problems like this is how to learn a new language. Thanks again the for help