Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayerArray addO

NSMutableArray
 
- (void)awakeFromNib { // initialization matrix for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { matrix[i][j] = 0; } } // Creating NSMutableArray instance TGrid = [NSMutableArray arrayWithCapacity:10]; [self saveGrid]; } - (void)saveGrid { NSNumber *aInt; NSMutableArray *Grid = [NSMutableArray arrayWithCapacity:81]; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { aInt = [NSNumber numberWithInt:matrix[i][j]]; [Grid addObject:aInt]; } } [TGrid addObject:Grid]; } - (IBAction)undo:(id)sender { [TGrid removeLastObject]; NSMutableArray *Grid = [TGrid lastObject]; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { matrix[8-i][8-j] = [[Grid lastObject] intValue]; [Grid removeLastObject]; } } }

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayerArray addObject:]: unrecognized selector sent to instance 0x4b36ce0


TGrid = [[NSMutableArray alloc] initWithCapacity:10] is a more conventional way to create the instance

arrayWithCapacity gives an autorelease object

Best option being to create a property with retain attribute out of it and access self.TGrid Don't forget to release it at the end (dealloc)


You need to retain TGrid ! Otherwise, it will be deallocate by the autorelease pool and probably a CALayer takes its place in the memory

你可能感兴趣的:(Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayerArray addO)