Search code examples
iphoneobjective-cjsonnsmutablearraynsarray

How does the objective-c SBJson library map an array? (do you recommend it?)


SBJsonParser *parser = [[SBJsonParser alloc] init];
NSMutableArray *componenti = [parser
                               objectWithString:@"[\"Item1\",\"Item2\"]"];
NSAssert([componenti isMemberOfClass:[NSMutableArray class]],@"err");

This code gives me an assertion failed. What is wrong?

The header file says:

@brief The JSON parser class.

JSON is mapped to Objective-C types in the following way:

@li Null -> NSNull
@li String -> NSMutableString
@li Array -> NSMutableArray
etc...

Solution

  • use NSAssert([componenti isKindOfClass:[NSMutableArray class]],@"err"); instead.

    I never really investigated this behavior but it seems that every time you instantiate a NSMutableArray, you received an instance of __NSArrayM in return. __NSArrayM is a subclass of NSMutableArray.

    And btw, SBJson is an excellent parser and I use it for a while now.