Joe
Joe

Reputation: 207

Inheritance Issues in Objective C

I created an "SDMutableGrid" class so that I could use a grid. It's just a child of NSMutableArray that contains a number for arrays equal to the number of rows in the grid.

Currently, the program quits before it really starts and it appears that it is because the methods defined for NSMutableArray somehow do not apply to SDMutableGrid, anyone know why?

Here is the .h :

#import <Foundation/Foundation.h>
#import "SDDimensions.h"

@interface SDMutableGrid : NSMutableArray {
SDDimensions dimensions;
}

@property (nonatomic) SDDimensions dimensions;

- (id)initWithDimensions:(SDDimensions)newDimensions;
- (void)addObject:(id)anObject toRow:(NSUInteger)row;

@end

Here is the .m :

#import "SDMutableGrid.h"

@implementation SDMutableGrid

@synthesize dimensions;

- (void)setDimensions:(SDDimensions)newDimensions {
if (newDimensions.width < dimensions.width) {
    NSMutableArray *anArray;
    NSRange aRange = NSMakeRange(newDimensions.width, dimensions.width - newDimensions.width);
    for (NSUInteger i = 0; i < MIN(dimensions.height,newDimensions.height); i++) {
        anArray = [self objectAtIndex:i];
        [anArray removeObjectsInRange:aRange];
    }
}
dimensions.width = newDimensions.width;
if (newDimensions.height > dimensions.height) {
    for (NSUInteger i = dimensions.height; i < newDimensions.height; i++) {
        [self addObject:[[NSMutableArray alloc] initWithCapacity:dimensions.width]];
    }
} else if (newDimensions.height < dimensions.height) {
    [self removeObjectsInRange:NSMakeRange(newDimensions.height, dimensions.height - newDimensions.height)];
}
dimensions.height = newDimensions.height;
}

- (id)initWithDimensions:(SDDimensions)newDimensions {
if (self = [super initWithCapacity:newDimensions.height]) {
    NSMutableArray *anArray;
    for (NSUInteger i = 0; i < newDimensions.height; i++) {
        anArray = [[NSMutableArray alloc] initWithCapacity:newDimensions.width];
        NSLog(@"Got this far");
        [self addObject:anArray];
        NSLog(@"woot");
        [anArray release];
    }
    NSLog(@"Finished Initializing grid");
}
return self;
}

- (void)addObject:(id)anObject toRow:(NSUInteger)row {
    [[self objectAtIndex:row] addObject:anObject];
}

@end

And here is what is appearing on the console:

2009-08-12 15:27:02.076 Flipswitch[1756:20b] Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ' -[NSMutableArray initWithCapacity:]: method only defined for abstract class. Define -[SDMutableGrid initWithCapacity:]!' 2009-08-12 15:27:02.080 Flipswitch[1756:20b] Stack: ( 807902715, 2536648251, 808283725, 808264737, 13690, 11018, 10185, 814713539, 814750709, 814739251, 814722434, 814748641, 839148405, 807687520, 807683624, 814715661, 814752238, 10052, 9906 )

Upvotes: 6

Views: 4207

Answers (4)

bobDevil
bobDevil

Reputation: 29538

This is due to the nature of 'Class Clusters' used for collection classes in Foundation. See: Class Clusters

Basically, NSMutableArray defines a public interface to 'mutable arrays', but is not the actual class you use when initialized. So 'initWithCapacity:' is defined, but not implemented in NSMutableArray. If you run:

NSMutableArary *foo = [[NSMutableArray alloc] init];
NSLog(@"%@", [foo className]);

you will print "_NSCFArray", which is a concrete subclass of NSMutableArray (and NSArray). To work around this, I would have a instance variable that is an NSMutableArray, or implement 'initWithCapacity:' with a suitable meaning (such as a capaciy of '3' means a 3x3 grid).

Upvotes: 7

Chuck
Chuck

Reputation: 237110

The short, easy answer: Don't make a subclass of NSArray. It's better to make a category on NSArray or make an NSObject subclass that has an NSArray ivar that you talk to.

The long, technical answer: NSArray is a class cluster. This means that it isn't actually one class, but many classes operating under the NSArray abstract class interface that are each implemented in a different way (say, one implementation for small arrays, another for big arrays, etc.). To create a subclass of a class cluster, you have to implement all the primitive methods of the abstract class you are inheriting from, manage your own storage and basically reimplement all the stuff you were hoping to get for free by subclassing.

More simply, you could just create a category if you don't require additional ivars. If you want an object that behaves like an array with additional state, you can create a class that has an NSArray and use Objective-C message forwarding to forward everything except your custom behavior to that class.

Upvotes: 38

Daniel
Daniel

Reputation: 22405

You problem is that you are not implementing abstract methods of NSMutableArray super class that need to be implemented, it says -[NSMutableArray initWithCapacity:]: method only defined for abstract class. Define -[SDMutableGrid initWithCapacity:]!' 2009-08-12 15:27:02.080 Flipswitch[1756:20b]

So you need do define initWithCapacity in your subclass, I would recommend to not extend NSMutableArray, there is no need, just make a class that has a mutable array in it.

Upvotes: 2

Joe
Joe

Reputation: 207

Ok, I found the answer from this question

Although the questions are different, the answer is the same and that is that due to the setup of NSArray (and therefore NSMutableArray), you cannot subclass it without implementing the methods yourself.

So I guess I'll just make SDMutableGrid have an NSMutableArray variable instead of actually being an NSMutableArray.

Upvotes: 2

Related Questions