超出NSArray的空白的边界异常 [英] Out of Bounds Exception on NSArray thats Empty

查看:149
本文介绍了超出NSArray的空白的边界异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码来创建一个图像数组,以便在视图上显示。我的视图中的大多数页面将有20个图像(5列,4列),但我实际上有43个图像,当我的图像数组包含最后3个时,我得到一个例外,当内部的第4次迭代循环,数组为空。

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];
    }
}

....

我可以绕过这个吗?

谢谢。

编辑:

从这个代码开始,是从数组中实际提取图像的代码。但是同样的情况发生在第四次迭代中,它崩溃了。

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

推荐答案

尝试使用实际的数组大小而不是 for 循环文本表达式。所以而不是这样:

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

执行此操作:

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

如下所示:

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];
    }
}

顺便说一下,嵌套的邮件表达式可以很酷,但嵌套调用 objectAtIndex:可能永远不是一个好主意。

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

这篇关于超出NSArray的空白的边界异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆