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 )