On 17 Nov 2019, at 15:08, Riccardo Mottola
<address@hidden> wrote:
I wanted to subclass NSMutableArray, so that I can easily add some
extra methods.
I declared my subclass like this:
@interface FileArray : NSMutableArray
{}
However, when I run my app, I get this:
StepSync.app/StepSync: Uncaught exception NSInvalidArgumentException,
reason: [FileArray-addObject:] should be overridden by subclass
I did override all concrete methods in the easiest possible way,
calling super. For addObject I did:
- (void)addObject:(id)anObject
{
[super addObject:anObject];
}
why is this not enough or not working in any case?
Because NSMutable array is an abstract class. You need to create a
concrete class with actual instance variables to store data in.
What you could have done to get the effect you seem to want is have a
concrete instance do the work for you:
@interface FileArray : NSMutableArray
{
NSMutableArray *content;
}
@end
@implementation FileArray
- (void) addObject: (id)anObject
{
[content addObject: anObject];
}
- (void) dealloc
{
[content release];
[super dealloc];
}
- (id) initWithCapacity: (NSUInteger)capacity
{
content = [[NSMutableArray alloc] initWithCapacity: capacity];
return self;
}
etc.
In this case the line
content = [[NSMutableArray alloc] initWithCapacity: capacity];
will actually create a GSMutableArray instance (a concrete subclass)
that your class implementation can use to store the objects.