Re: Subclass of NSMutableArray isn't working
Re: Subclass of NSMutableArray isn't working
- Subject: Re: Subclass of NSMutableArray isn't working
- From: "Louis C. Sacha" <email@hidden>
- Date: Wed, 10 Mar 2004 12:25:41 -0800
Hello...
If you are only using [[Graph alloc] init] to create your Graph
instances, then they should always start out as Graph instances and
not NSCFArray instances.
I'm fairly certain that none of the +(id)array... methods are being
called anywhere, because they would be raising exceptions unless you
have provided your own implementations, and you would probably have
noticed them in the NSLog output. The exceptions raised by those
methods are hard coded into the NSArray implementation of the various
specialized init... methods to warn you if any of those methods are
being called (and the +array... methods usually use those specialized
init... methods as part of their implementation).
Calling [super init] within your Graph init method isn't causing the
problem, because NSArray (and NSMutableArray) do not actually
implement the init method, and calling the superclass version just
results in the NSObject version of init being called.
The only cause that I can think of at the moment is that if you are
using the copy method, or passing your Graph instances to any object
that might use copy on them, it might be the source of your problem.
You would need to implement your own version of copyWithZone: (which
is the actual method that does the work whenever copy is called) and
mutableCopyWithZone:, since the NSArray versions are probably hard
coded to return an NSCFArray instance.
/* this assumes that there is no such thing as an immutable Graph, so
all copies would be mutable */
- (id)copyWithZone:(NSZone *)zone
{
Graph *duplicate = [[Graph alloc] init];
unsigned i, objectCount = [self count];
for (i=0; i < objectCount; i++)
{
[duplicate addObject:[self objectAtIndex:i]];
}
return duplicate;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
return [self copyWithZone:zone];
}
You probably would also have a similar problem with the NSArray
implementations of the methods for the NSCoding protocol if you ever
archive your graph objects, and to fix that you would need to write
your own initWithCoder: and encodeWithCoder: methods, as well as
override the classForCoder instance method to return [Graph class].
Hope that helps,
Louis
I have implemented a handful of my own methods to extend the
NSMutableArray class such as:
-(BOOL)validateGraph;
However, at runtime I receive the error:
-[NSCFArray validateGraph]: selector not recognized
In essence, instances of Graph are acting just like mutable arrays
and seem to be ignoring all of my methods in the Graph subclass. I
have a feeling that it has to do with the +(id)array... methods
which I have not overridden. I suspect that they are returning
arrays rather than graphs, but I did try overriding one and using
typecasting to force it to be a Graph, this just gave me a compiler
error. I have been using [[Graph alloc]init] to make graphs but the
+(id)array... methods must be getting called anyways. I also wonder
if my -(id)init method should not call [super init] because it is a
class cluster.
_______________________________________________
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.