Initialize NSMutableArray: [NSMutableArray array];
Solution 1
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
).
Solution 2
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]));
Comments
-
Corey Floyd almost 2 years
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)
-
Corey Floyd almost 15 yearsahh, interesting. so self references the Class Object: NSMutableArray. That makes sense. I wondered why my code wasn't breaking!
-
Anatoly Ivanov almost 15 yearsI think you actually would do: return [[[self alloc] init] autorelease];
-
Jim Dovey almost 15 yearsSurely that would always return
NSCFArray
? -
Corey Floyd almost 15 yearsHmmm 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.
-
danielpunkass almost 15 yearsYeah, 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!
-
Corey Floyd almost 15 yearswow. 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