开发者

Out of Bounds Exception on NSArray thats Empty

I have this code for creating an array of images to display on a view. Most of the pages in my view will have 20 images (in columns of 5, rows of 4), but i actually have 43 images and when my images array contains the final 3, i get an exception when on the 4th iteration of the inner loop, the array is empty.

- (void)displayImages:(NSMutableArray *)images {

NSMutableArray *keysArray = [[NSMutableArray alloc] initWithCapacity:5];

for (int column = 0; column < 5; column++){
    [keysArray  addObject:[NSMutableArray arrayWithCapacity:5]];
    for (int row = 0; row < 4; row++) {
        [[keysArray objectAtIndex:column] addObject:[images objectAtIndex:0]];
        [images removeObjectAtIndex:0];
    }
}

....

Can i get around this?

Thanks.

EDIT:

Following on from this code, is the code which actually pulls the image from the array. But the same scenario occurs... on the fourth iteration it crashes.

for (int column = 0; column < 5; column++) {
    for (int 开发者_C百科row = 0; row < 4; row++){          
        UIButton *keyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        keyButton.frame = CGRectMake(column*kKeyGap, row*kKeyGap, kKeySize, kKeySize);

        [keyButton setImage:[[keysArray objectAtIndex:column] objectAtIndex:row] forState:UIControlStateNormal];
        [keyButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:keyButton];
    }
}
[keysArray release];

I can't test images at this point as the array is emptied already.


Just check to see if there are objects in that array before you try to do anything with it

- (void)displayImages:(NSMutableArray *)images { 

NSMutableArray *keysArray = [[NSMutableArray alloc] initWithCapacity:5]; 

for (int column = 0; column < 5; column++){ 
    [keysArray  addObject:[NSMutableArray arrayWithCapacity:5]]; 
    for (int row = 0; row < 4; row++) { 
        //test to see if there's an object left in the inner array
        if ([images count] > 0) {
            [[keysArray objectAtIndex:column] addObject:[images objectAtIndex:0]]; 
            [images removeObjectAtIndex:0];
        }
    } 
} 


Try using the actual array sizes instead of constant values in your for loop text expressions. So rather than this:

for (int column = 0; column < 5; column++)

do this:

for (int column = 0; column < [keysArray count]; column++)

The resulting code might look something like this:

for (int column = 0; column < [keysArray count]; column++) {

    NSArray *rowArray = [keysArray objectAtIndex:column];

    for (int row = 0; row < [rowArray count]; row++) {          
        UIButton *keyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        keyButton.frame = CGRectMake(column*kKeyGap, row*kKeyGap, kKeySize, kKeySize);

        [keyButton setImage:[rowArray objectAtIndex:row] forState:UIControlStateNormal];
        [keyButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:keyButton];
    }
}

By the way, nested message expressions can be cool sometimes, but nesting calls to objectAtIndex: is probably never a good idea.


test the number of object in images

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜