views:

2750

answers:

2

If you initialize an NSMutableArray with NSArray's convenience method as above, do you get an NSArray or an NSMutableArray?

Any consequences?

(I know that NSMutableArray has "arrayWithCapacity:, I'm just curious)

+7  A: 

If you initialize it using:

NSMutableArray *array = [NSMutableArray array];

you get a NSMutableArray. One great feature of Objective-C is that class methods are inherited by subclasses.

So, in a class method you can do something like this:

+(id)array {
    return [[[self alloc] init] autorelease];
}

and self will be referencing to the class object where the code is executing (NSArray or NSMutableArray).

pgb
ahh, interesting. so self references the Class Object: NSMutableArray. That makes sense. I wondered why my code wasn't breaking!
Corey Floyd
I think you actually would do: return [[[self alloc] init] autorelease];
Don McCaughey
You are right, I'll change the sample code.
pgb
+4  A: 

Update: While my advice to "test it yourself" is generally a good idea, it was a little trigger-happy in this case. Thanks to Jim in the comments for pointing at that my suggestion below doesn't work well for these classes, because the various forms of NSArray are all implemented by a CoreFoundation toll-free bridging class.

----- Original Answer For Context Below -----

The easiest way to answer a question like this is to test it yourself. Try allocating the array in the way you were curious about, then NSLog from your code:

NSLog(@"We got a %@", NSStringFromClass([theArray class]));

danielpunkass
Surely that would always return `NSCFArray`?
Jim Dovey
Great point, Jim. I forgot about that peculiarity of NSArray. Editing my response above. Thanks.
danielpunkass
Hmmm intersesting. So even an NSMutableArray will spit back a NSCFArray? That makes it hard to do introspection and find out if you have a mutable object before you try change it. I guess you can do respondsToSelector in that case. On a side note thanks for your awesome podcast. I have went back and listened to most of your and Manton's past episodes. Pretty entertaining and informative.
Corey Floyd
Yeah, using respondsToSelector seems like a reasonable approach there. Maybe there's a better way to inspect for mutability, I don't know.Glad you are enjoying the podcast!
danielpunkass
Sadly that's not possible. NSCFArray implements one set of methods, and it doesn't alter that list if it happens to be a mutable instance. All part of the funky toll-free-bridging stuff: NSCFArray is actually an ObjC class description applied to CFArrayRef instances-- in other words, create a CFArrayRef, call [ref class], and it'll hand back NSCFArray.The only way I know to reliably test for mutability is to wrap it in a try/catch block and handle the NSInternalInconsistencyException that'll be raised if it's not really mutable. That or dig into the CF source )
Jim Dovey
wow. I can't think of an instance when I will actually wouldn't know if I was dealing with a mutable object, but that is definitely a nice dance just to determine mutability. This was more just an intellectual exercise, but this does reveal a weird issue you could run in to and waste several hours on
Corey Floyd