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
Jeff Ripke
41,989 PointsWhat should I put for arguments for NSJSONSerialization method?
I am getting an error when I use the following arguments:
let jsonDictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? [String: AnyObject]
completion(jsonDictionary)
1 Answer
Rick Heffren
4,312 PointsHi, I'm having the same problem. From what I can gather on the internet; the error parameter has been removed in Swift 2.0 for JSONObjectWithData and instead of using nil in the options: parameter we now need to use '[]'. Also since the error parameter has been removed we now use the 'try' operator in front of NSJSONSerialization. However this doesn't work. I'm assuming because we are in an 'if let' statement and we know that this statement won't 'throw' and error, therefore we now need to use the bang operator to force the 'try!'. Confusing? I think so. Anyhow here's the code that works for me:
let jsonDictionary = try! NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: AnyObject]
As of Nov 18, 2015 I've noted quite a few discrepencies with the latest version of Swift.