7

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)

1
  • 1
    I appreciate the interesting discussion below for a simple question. It brings to bear a few subtle Cocoa points. Commented May 12, 2009 at 2:22

2 Answers 2

11

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).

Sign up to request clarification or add additional context in comments.

2 Comments

ahh, interesting. so self references the Class Object: NSMutableArray. That makes sense. I wondered why my code wasn't breaking!
I think you actually would do: return [[[self alloc] init] autorelease];
4

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]));

6 Comments

Surely that would always return NSCFArray?
Great point, Jim. I forgot about that peculiarity of NSArray. Editing my response above. Thanks.
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.
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!
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 & check the CF header bits ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.