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

  • Home
  • SEARCH
  • 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 8736289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:16:21+00:00 2026-06-13T10:16:21+00:00

After parsing the JSON response from a webservice using NSJSONSerialization , I use +isKindOfClass:

  • 0

After parsing the JSON response from a webservice using NSJSONSerialization, I use +isKindOfClass: to make sure the server returned the kind of data I expect. Using this method, I ran into some weird behaviour, which I’ll illustrate using an example.

Consider the following objects:

// Definitions
NSDictionary *son = @{ @"firstname" : @"James", @"lastname" : @"Appleseed" };
NSDictionary *daughter = @{ @"firstname" : @"Susan", @"lastname" : @"Appleseed"};
NSArray *children = @[son, daughter];
NSDictionary *father = @{ @"firstname" : @"John", @"lastname" : @"Appleseed" };
NSDictionary *family = @{@"children" : children, @"father" : father};
NSDictionary *pedigree = @{@"family" : family };

Those objects represent deserialized JSON returned from a server. Now if I want to use the array of children to calculate how much children there are using NSArray’s -count, I need to make sure the children object is an NSArray. If the children object for example happens to be a string, while the app expects an array, it’ll crash because strings don’t implement a count method. Consider the following code sequence which implements the described check:

// First test
id _family = [pedigree objectForKey:@"family"];
if ([_family isKindOfClass:[NSDictionary class]])
{
    NSDictionary *_family = (NSDictionary *)_family;
    id _children = [_family objectForKey:@"children"];

    NSLog(@"Children: %@", _children);
    NSLog(@"Children classname: %@", NSStringFromClass(children.class));

    if ([_children isKindOfClass:[NSArray class]]) {
        NSLog(@"Children is an NSArray");
    } else {
        NSLog(@"Children is not an NSArray");
    }
} else {
    NSLog(@"Family is not an NSDictionary");
}

After running this code, the console outputs the following:

Children: (null)
Children classname: __NSArrayI
Children is not an NSArray

The console output appears to be extremely remarkable and even contradictory. How could children not be an NSArray while its classname is __NSArrayI?

After a bit of debugging, I found that there are two ways to solve this problem:

  1. remove this line NSDictionary *_family = (NSDictionary *)_family;
  2. use a different name than _family for the casted variable

How could this behaviour be explained?

  • 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-13T10:16:22+00:00Added an answer on June 13, 2026 at 10:16 am

    In the line

    NSDictionary *_family = (NSDictionary *)_family;
    

    you define a new variable _family in the current scope, which makes the outer variable _family invisible. Objective-C pointers are initialized to nil if you compile with ARC.

    And the output is not contradictory, because you print

    NSStringFromClass(children.class);
    

    which is the class of children (without the underscore), which is an array. But _children (with underscore) is nil because _family is nil as explained above.

    In fact you don’t need a type cast if you expect a dictionary. You could just do

    NSDictionary *_family = [pedigree objectForKey:@"family"];
    if ([_family isKindOfClass:[NSDictionary class]])
    {
        NSArray *_children = [_family objectForKey:@"children"];
    
        if ([_children isKindOfClass:[NSArray class]]) {
            NSLog(@"Children is an NSArray");
        } else {
            NSLog(@"Children is not an NSArray");
        }
    } else {
        NSLog(@"Family is not an NSDictionary");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I make an ajax request, and the JSON response, after parsing, gives me the
I am generating a list of check boxes, after parsing it from a JSON.
I'm trying to use external api and display its results (after json parsing) on
after messing around with parsing a JSON response with GSON for a day, I
I need to create a dynamic html table using PHP after parsing a JSON
I can't seem to access my objects. after parsing the server string: var json
For parsing JSON like this twitter API users/show response I've been using Jackson and
I'm creating an app that fetches back a Json response from a webservice. The
I'm retreiving a JSON string and parsing it with jQuery with $.getJSON. After I
After completing the XML SAX Parsing now I am working on JSON Parsing in

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.