Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 )

like image 488
Joe Avatar asked Aug 12 '09 19:08

Joe


1 Answers

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.

like image 110
Chuck Avatar answered Oct 15 '22 13:10

Chuck