gnustep-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Issues subclassing NSMutableArray


From: Richard Frith-Macdonald
Subject: Re: Issues subclassing NSMutableArray
Date: Sun, 17 Nov 2019 15:53:09 +0000


> On 17 Nov 2019, at 15:08, Riccardo Mottola <address@hidden> wrote:
> 
> Hi!
> 
> 
> 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.
In your case you overrode the superclass implementation, but did do by calling 
the superclass implementation ... which is abstract and can't do anything.  You 
have to override the implementation in a way that actually adds the object to a 
storage area.


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.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]