I\'m trying to take datas from a url (json file) I get this error on these lines:
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJS
Whatever the JSON file data looks like, the top level object is an array. Because you passed .MutableContainers
for the options:
argument, the deserialization returns you a mutable array.
You are force-casting this to an NSDictionary
:
as! NSDictionary
But you can't do that because it's an array, not a dictionary.
The proper thing to do depends entirely on what we're writing code for.
If we're not, we need a more dynamic approach. But if we are, this error makes it clear that you're deserializing an array, so let's change as! NSDictionary
to:
as NSMutableArray
This will do several things.
Since we're bothing to grab mutable objects, this will give us mutable objects back (otherwise we shouldn't read them as mutable).
We'll actually read the right type back (an array versus a dictionary).
And by removing the !
, we'll get back an optional. Good news is that this means that our code won't crash just because we got unexpected JSON.