Here's the code:
NSError *parseError;
NSMutableArray *listOfObjects = [NSJSONSerialization JSONObjectWithData:[@"[]" dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&parseError];
NSLog(@"Is mutable? %li", [listOfObjects isKindOfClass:[NSMutableArray class]]);
listOfObjects = [NSJSONSerialization JSONObjectWithData:[@"[[],{}]" dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&parseError];
NSLog(@"Is mutable? %li", [listOfObjects isKindOfClass:[NSMutableArray class]]);
As you can see, I'm calling exactly the same method for parsing the JSON both times, one with an empty list in the JSON, and then a list with an object inside. Here's the result:
Is mutable? 0
Is mutable? 1
The problem is that the NSJSONSerialization doesn't seem to follow the option to create mutable containers for empty lists. Seems like a bug to me, but maybe I just misunderstanding things.
Any ideas?
Here is what I do:
Here's my workaround for this problem:
So I call:
Instead of the usual:
Others also take this for a bug, see
In the latter case you can see complete workaround also for empty dictionaries (see
DynamicGetter(...)
method).This works just as expected:
Here's the console output:
EDIT
Note that if we append the following line to the code above...
...we get the following output on the console:
...just in case it wasn't clear that
__NSArrayM
is a private subclass ofNSMutableArray
, thus proving that the OP's code did indeed work as expected (except for hisNSLog
statement).EDIT
Oh, and by the way, the following line of code...
...results in the following console output:
EDIT (responding to changed question)
Interesting...looks like a bug. Given the following code:
Here's the console output:
So empty arrays and dictionaries created this way don't seem to behave as expected.