NSMutableArray : 无法识别的选择器发送到实例
我正在尝试使用 NSMutableArray 的 NSMutableArray 存储数组 int[9][9],其中我存储了数组中的 81 个整数:
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];
}
}
}
当 saveGrid 第一次被awakeFromNib 方法调用时,它起作用了.但是当我更改矩阵时,它再次调用 saveGrid,这次我收到此错误:
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 :
* 由于未捕获的异常而终止应用'NSInvalidArgumentException',原因:'-[CALayerArray addObject:]:无法识别的选择器发送到实例0x4b36ce0'
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayerArray addObject:]: unrecognized selector sent to instance 0x4b36ce0'
我需要你的帮助!谢谢!
I need your help ! Thanks !
你需要保留 TGrid !否则,它会被自动释放池解除分配,并且可能会有一个 CALayer 在内存中占据它的位置!
You need to retain TGrid ! Otherwise, it will be deallocate by the autorelease pool and probably a CALayer takes its place in the memory !
最好的选择是创建一个带有保留属性的属性并访问 self.TGrid
最后别忘了释放(dealloc
)
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
)
编辑它被释放是因为每个实例创建者类方法都提供了 Autoreleased 实例(这是每个人都应该遵循的规则,Apple 在整个 SDK 中都遵循).
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).