RE: Class Cluster Confusion
RE: Class Cluster Confusion
- Subject: RE: Class Cluster Confusion
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Fri, 20 Jun 2003 14:02:36 -0400
There's nothing particularly weird going on. You just got lucky.
The thing about clusters is that how they handle your alloc requests is a
total black-box mystery; you can't count on getting any particular subclass
when you alloc an instance of the class. Sometimes NSArray (or any method
that returns an array -- even one of yours) might spit out a mutable
subclass (perhaps cast to NSArray, but nonetheless staying mutable) because,
for whatever reason, it's the cheapest alternative in terms of time and
resources.
Nonetheless, your code cannot count on getting any particular subclass. My
understanding (and someone please correct me if I'm wrong) is that NSArray
(and NSMutableArray) decide which subclass to alloc at runtime, depending on
the conditions in the application as it is running. A two-member array of
objects from the same class might get one kind of NSArray subclass, while a
2 million-member array of many different kinds of objects might get a
completely different one.
Jonathan
>
-----Original Message-----
>
From: email@hidden
>
[mailto:email@hidden]On Behalf Of Gerriet M. Denkmann
>
Sent: Thursday, June 19, 2003 7:57 AM
>
To: email@hidden
>
Subject: Class Cluster Confusion
>
>
>
NSFileManager *defaultManager = [ NSFileManager defaultManager ];
>
id dirContent = [ defaultManager directoryContentsAtPath: aPath ];
>
NSLog(@"dirContent class: \"%@\"", [ dirContent class ] );
>
>
// class is NSCFArray, which is also what the documentation tells me.
>
>
// this is a method of NSMutableArray, not of NSArray:
>
[ dirContent removeObjectAtIndex: 0 ] ;
>
>
Now I would expect a run-time error: "-[NSCFArray
>
removeObjectAtIndex:]: selector not recognized", but it just works.
>
Why?
>
>
// Then I did:
>
NSMutableArray *mutants = [ [ NSMutableArray alloc ] initWithCapacity:
>
count ] ;
>
NSLog(@"mutants class: \"%@\"", [ mutants class ] );
>
// class is again NSCFArray
>
>
Does this mean that there is no such thing as a NSMutableArray? That
>
this is always the same as NSArray? That the distinction is only for
>
static type checking?
>
>
Or what am I misunderstanding here?
>
>
Gerriet.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.