Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7718383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:09:17+00:00 2026-06-01T03:09:17+00:00

Here’s the code: NSError *parseError; NSMutableArray *listOfObjects = [NSJSONSerialization JSONObjectWithData:[@[] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&parseError]; NSLog(@Is

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T03:09:19+00:00Added an answer on June 1, 2026 at 3:09 am

    This works just as expected:

    NSString *s = @"{ \"objs\": [ \"a\", \"b\" ] }";    
    NSData *d = [NSData dataWithBytes:[s UTF8String] length:[s length]];
    id dict = [NSJSONSerialization JSONObjectWithData:d options:NSJSONReadingMutableContainers error:NULL];
    
    NSLog(@"%@", dict);
    
    [[dict objectForKey:@"objs"] addObject:@"c"];
    
    NSLog(@"%@", dict);
    NSLog(@"%@", [[dict objectForKey:@"objs"] class]);
    

    Here’s the console output:

    2012-03-28 13:49:46.224 ExampleRunner[42526:707] {
        objs =     (
            a,
            b
        );
    }
    2012-03-28 13:49:46.225 ExampleRunner[42526:707] {
        objs =     (
            a,
            b,
            c
        );
    }
    2012-03-28 13:49:46.225 ExampleRunner[42526:707] __NSArrayM
    

    EDIT

    Note that if we append the following line to the code above…

    NSLog(@"%@", [[dict objectForKey:@"objs"] superclass]);
    

    …we get the following output on the console:

    2012-03-28 18:09:53.770 ExampleRunner[42830:707] NSMutableArray
    

    …just in case it wasn’t clear that __NSArrayM is a private subclass of NSMutableArray, thus proving that the OP’s code did indeed work as expected (except for his NSLog statement).

    EDIT

    Oh, and by the way, the following line of code…

    NSLog(@"%d", [[dict objectForKey:@"objs"] isKindOfClass:[NSMutableArray class]]);
    

    …results in the following console output:

    2012-03-28 18:19:19.721 ExampleRunner[42886:707] 1
    

    EDIT (responding to changed question)

    Interesting…looks like a bug. Given the following code:

    NSData *dictData2 = [@"{ \"foo\": \"bar\" }" dataUsingEncoding:NSUTF8StringEncoding];
    id dict2 = [NSJSONSerialization JSONObjectWithData:dictData2 options:NSJSONReadingMutableContainers error:NULL];
    NSLog(@"%@", [dict2 class]);
    NSLog(@"%@", [dict2 superclass]);
    NSLog(@"%d", [dict2 isKindOfClass:[NSMutableDictionary class]]);
    
    // This works...
    [dict2 setObject:@"quux" forKey:@"baz"];
    NSLog(@"%@", dict2);
    
    NSData *dictData = [@"{}" dataUsingEncoding:NSUTF8StringEncoding];
    id emptyDict = [NSJSONSerialization JSONObjectWithData:dictData options:NSJSONReadingMutableContainers error:NULL];
    NSLog(@"%@", [emptyDict class]);
    NSLog(@"%@", [emptyDict superclass]);
    NSLog(@"%d", [emptyDict isKindOfClass:[NSMutableDictionary class]]);
    
    //...but this fails:
    [emptyDict setObject:@"quux" forKey:@"baz"];
    NSLog(@"%@", emptyDict);
    

    Here’s the console output:

    2012-03-29 09:40:52.781 ExampleRunner[43816:707] NSMutableDictionary
    2012-03-29 09:40:52.782 ExampleRunner[43816:707] 1
    2012-03-29 09:40:52.782 ExampleRunner[43816:707] __NSCFDictionary
    2012-03-29 09:40:52.782 ExampleRunner[43816:707] NSMutableDictionary
    2012-03-29 09:40:52.783 ExampleRunner[43816:707] 1
    2012-03-29 09:40:52.783 ExampleRunner[43816:707] {
        baz = quux;
        foo = bar;
    }
    2012-03-29 09:40:52.784 ExampleRunner[43816:707] __NSCFDictionary
    2012-03-29 09:40:52.784 ExampleRunner[43816:707] NSMutableDictionary
    2012-03-29 09:40:52.784 ExampleRunner[43816:707] 1
    2012-03-29 09:40:52.785 ExampleRunner[43816:707] NSException: -[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object
    

    So empty arrays and dictionaries created this way don’t seem to behave as expected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my code, which takes two version identifiers in the form 1, 5,
Here is an example: I write html code inside of textarea, then I swap
Here is my code...I have two dimensional matrices A,B. I want to develop the
Here is my code sample, let me know if it can be further improved?
Here is my code (Say we have a single button on the page that
here are 2 screen shots when i try to debug my code in visual
Here a simple question : What do you think of code which use try
Here is some code I made :) @echo off set source=R:\Contracts\ set destination=R:\Contracts\Sites\ ROBOCOPY
Here my code: $(document).ready(function() { $('#mid_select').live('click', function(e){ $('#middle').load( $(this).attr('href') + ' #middle'); var page
Here is some code on the javascript side for form-based uploads: iframe.setAttribute('src', 'javascript:false;'); I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.