0

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.

11684
  • 7,356
  • 12
  • 48
  • 71
  • I believe your array is going through a change somewhere down the line. Can you show us more code where you do work with that NSMutableArray? – Ryan Poolos Jun 26 '12 at 15:53
  • There is nothing else. That's the first thing I do with this array except for passing it into a method. @RyanPoolos – 11684 Jun 26 '12 at 16:00
  • Then can we see the method? My guess is your method is the problem then. can we see is interface and implementation. – Ryan Poolos Jun 26 '12 at 16:47

2 Answers2

1

Your check is incorrect. It should be either

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

or

if ([expr isKindOfClass:[NSMutableArray class]]) {
Alexander
  • 8,117
  • 1
  • 35
  • 46
  • I'll accept in 8 minutes. But another question: As you see I put NSStrings and NSNumbers in there, but I get them back as NS**CF**Strings and NS**CF**Numbers. Why? – 11684 Jun 26 '12 at 15:52
  • That's the core foundation format. There's a toll free bridging between Cocoa formats and Core foundation formats (i.e. they are functionally equal). You can read more [here](https://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/tollFreeBridgedTypes.html#//apple_ref/doc/uid/TP40010677-SW1) – Alexander Jun 26 '12 at 15:54
  • So you say I can do everything that I can do with a string with an NSCFString? – 11684 Jun 26 '12 at 15:58
  • Yes, check out the link at the end of my last comment. – Alexander Jun 26 '12 at 15:59
1

The answer lies in isKindOfClass - clearly you would want to execute the code reserved for mutable arrays if you created a subclass of an NSMutableArray.

In the same way, there must be some internal Cocoa implementation detail where the class of the the mutable array that you made is different from [NSMutableArray class]. I don't know the reason, but you should not care either, you almost always want to use isKindOfClass - which works for subclasses too.

If you do care to find out...

Its all about class clusters - see Objective c isKindOfClass missunderstanding?

Community
  • 1
  • 1
Tom Andersen
  • 7,132
  • 3
  • 38
  • 55