Search code examples
objective-cintrospection

Why isn't my NSMutableArray of type NSMutableArray?


I got an NSMutableArray, created like this:

NSMutableArray *expr = [[NSMutableArray alloc] init]; 
    [expr addObject:[NSNumber numberWithDouble:3.0]];
    [expr addObject:@"+"];
    [expr addObject:@"%x"];
    [expr addObject:@"*"];
    [expr addObject:[NSNumber numberWithDouble:4.0]];

Later on, I pass it to method where it is known as (id) anExpression, and I try to determine what kind of object this is with introspection:

if ([[anExpression class] isEqual:[NSMutableArray class]]) {
    // Some code I only want to do if it IS a Mutable array
}

But, that returns false, and my code isn't executed. I tried NSLog'ing [NSMutableArray class] and that gives me NSMutableArray. Not quite a surprise.
But when I try NSLog'ing expr's class, I get __NSArrayM.

Why is this? How should I get around this?

I am not that familiar with Objective-C, so please explain things. I don't like doing things I don't understand.


Solution

  • Your check is incorrect. It should be either

    if ([[expr class] isEqual:[NSMutableArray class]]) {
    

    or

    if ([expr isKindOfClass:[NSMutableArray class]]) {