NSMutableArray:无法识别的选择器发送到实例
我正在尝试使用 NSMutableArray 的 NSMutableArray 存储数组 int[9][9] ,其中存储数组中的 81 个整数:
- (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];
}
}
}
当 awakeFromNib 方法首次调用 saveGrid 时,它会起作用。但是当我更改矩阵时,它再次调用 saveGrid,这次我收到此错误:
* 由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因: '-[CALayerArray addObject:]: 无法识别的选择器发送到实例 0x4b36ce0'
我需要你的帮助! 谢谢 !
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:
'-[CALayerArray addObject:]:
unrecognized selector sent to instance
0x4b36ce0'
I need your help !
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要保留 TGrid !否则,它将被自动释放池释放,并且可能 CALayer 在内存中占据它的位置!
最好的选择是创建一个带有保留属性的属性并访问 self.TGrid
不要忘记在最后释放它(
dealloc
)编辑
它被释放是因为每个实例创建者类方法都提供了 Autoreleased 实例(这是每个人都应该遵循的规则,Apple 在整个 SDK 中也确实遵循了这一规则)。
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).