NSMutableArray : unrecognized selector sent to instance

青春壹個敷衍的年華 提交于 2019-12-11 02:53:04

问题


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 !


回答1:


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).



来源:https://stackoverflow.com/questions/4562556/nsmutablearray-unrecognized-selector-sent-to-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!