【问题标题】:Out of Bounds Exception on NSArray thats EmptyNSArray 上的越界异常是空的
【发布时间】:2010-11-23 11:37:36
【问题描述】:

我有这段代码用于创建要在视图上显示的图像数组。我视图中的大多数页面将有 20 个图像(5 列,4 行),但我实际上有 43 个图像,当我的图像数组包含最后 3 个时,我在内部的第 4 次迭代时出现异常循环,数组为空。

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

....

我可以解决这个问题吗?

谢谢。

编辑:

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

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

此时我无法测试images,因为数组已经清空了。

【问题讨论】:

    标签: objective-c arrays exception ios4


    【解决方案1】:

    在尝试对它进行任何操作之前,只需检查该数组中是否有对象

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

    【讨论】:

      【解决方案2】:

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

      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: 可能永远不是一个好主意。

      【讨论】:

        【解决方案3】:

        测试images中的对象个数

        【讨论】:

          猜你喜欢
          • 2016-06-19
          • 1970-01-01
          • 2014-11-19
          • 1970-01-01
          • 2012-03-31
          • 2018-06-07
          • 2013-11-28
          • 2013-03-23
          • 1970-01-01
          相关资源
          最近更新 更多