NSMutableArray : unrecognized selector sent to instance
I'm trying to store an array int[9][9] with a NSMutableArray of NSMutableArray where I store my 81 integers from the array :
- (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];
}
}
}
When saveGrid is first called by the awakeFromNib method, it works. But when I change my matrix, it calls again saveGrid, and this time I get this error :
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CA开发者_如何学GoLayerArray addObject:]: unrecognized selector sent to instance 0x4b36ce0'
I need your help ! Thanks !
You need to retain TGrid ! Otherwise, it will be deallocate by the autorelease pool and probably a CALayer takes its place in the memory !
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
)
edit It is deallocated because every instance creator class method provides Autoreleased instance (that's a rule that everyone should follow and that Apple does follow in the whole SDK).
精彩评论