【问题标题】:NSMutable Array beyond boundsNSMutableArray 越界
【发布时间】:2011-09-06 17:36:30
【问题描述】:

我有一段代码在执行时给了我这个错误。而且我比较新,我似乎无法解决问题。

错误: 2011-09-06 12:31:06.094 ForceGauge[266:707] CoreAnimation:忽略异常:*-[NSMutableArray objectAtIndex:]:索引 1 超出范围 [0 .. 0]

-(void)peakCollector:(NSMutableArray *)testarray {

    NSUInteger totalRows = [testarray count];

    NSMutableArray *percentArray = [[NSMutableArray alloc]initWithObjects:0, nil];

    if(forcecontroller.selectedSegmentIndex==0)
        testarray = lbData;
    else if(forcecontroller.selectedSegmentIndex==1)
        testarray = kgData;     
    else if(forcecontroller.selectedSegmentIndex ==2)
        testarray = ozData;
    else if(forcecontroller.selectedSegmentIndex ==3)
        testarray = newtonData;

    for(int i = 0; i< totalRows-1; i++) {

        if ([[testarray objectAtIndex:i+1] doubleValue] >= 1.2 * [[testarray objectAtIndex:i] doubleValue]) {
            percentArray = [testarray objectAtIndex:i];

            DatabaseTable *tableVC = [[DatabaseTable alloc] initWithStyle:UITableViewStylePlain];
            [self.navigationController pushViewController:tableVC animated:YES];

            if(forcecontroller.selectedSegmentIndex==0)
                [tableVC copydatabase:percentArray];
            else if(forcecontroller.selectedSegmentIndex==1)
                [tableVC copydatabase:kgData];
            else if(forcecontroller.selectedSegmentIndex==2)
                [tableVC copydatabase:ozData];
            else if(forcecontroller.selectedSegmentIndex==3)
                [tableVC copydatabase:newtonData];

            [tableVC release];
        } else {
            [analogData removeAllObjects];
        }
    }
}

【问题讨论】:

  • [[NSMutableArray alloc]initWithObjects:0, nil];。 NSArray 只能包含对象,不能包含原始类型,您需要将它们包装起来,例如[[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:0], nil];
  • @Bavarious 仍然给出同样的错误。
  • @ila 我觉得问题出在其他地方,这就是我删除答案的原因。不过,我写的(以及 albertamg 作为评论发布的)仍然有效。
  • 即使 '0' 值确实必须封装在 NSNumber 中,异常的原因在其他地方,隐藏在一个棘手的打字问题中:请参阅下面的答案。

标签: iphone ios nsmutablearray


【解决方案1】:

这里有多个问题:

1) NSArrays 只能包含 NSObjects。

在您的代码中,您正在使用[[NSMutableArray alloc]initWithObjects:0, nil]; 初始化您的 NSArray,但 0 是原子类型,而不是 NSObject (并且基本上 0 与 nil 的值相同(nil 和 NULL 通常等于 0,分别解释为 idvoid* 类型)

您必须将 0 值封装在 NSNumber 中:

[[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:0], nil];

然后使用[percentArray objectAtIndex:0]检索NSNumber,最后使用NSNumberintValue方法将检索NSNumber转换回int:

NSNumber* number = [percentArray objectAtIndex:0]; // returns an NSNumber which is an NSObject encapsulating numbers, see Apple's documentation
int val = [number intValue]; // retrieve the integer value encapsulated in the NSNumber
// or directly:
// int val = [[percentArray objectAtIndex:0] intValue];

2) 您得到的异常实际上在其他地方,而且更加微妙:您在 NSUInteger 变量中检索 [testarray count] 值,这是一个无符号类型。如果 totalRows 等于 0,totalRows-1 会做一些棘手的事情(考虑到你的例外情况显然是这种情况)。

由于totalRowsNSUInteger,当它等于0时,totalRows-1将不等于-1,而是...(NSUInteger)-1(-1解释为无符号整数),这是0xFFFFFFFF,也就是NSUInteger类型的最大值!

这就是为什么i 总是小于这个totalRows-1 值(因为这个值不是-1,而是0xFFFFFFFF = NSUInteger_MAX)。

要解决此问题,请将您的 totalRows 变量转换为 NSInteger 值,或在代码中添加一个条件以单独处理此特殊情况。

【讨论】:

  • 我修复了 NSUInteger,它消除了错误。现在我还有其他问题,我知道它现在没有超出循环。
  • 很高兴我解决了你的问题(因为看起来有点棘手!):)
  • ilaunchpad,你应该检查 AliSoftware 的答案的答案框给他的功劳。
  • @AliSoftWare 你能完成第一个任务吗?我总是对 NSNumber 问题感到困惑。 NSMutableArray *percentArray = [[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:0, nil]];
  • 您好,我不明白您的评论:我在上面的答案中发布的关于 NSNumber 的代码摘录对您来说不够清楚?
【解决方案2】:

请检查测试数组在 for 循环开始时包含多少个对象。

其他可以帮助的事情是避免使用 NSUInteger 并使用简单的 int 来存储数组计数。

如果这不起作用,请发布。

【讨论】:

    【解决方案3】:

    该错误仅表示您正在尝试在不存在的索引处检索对象。在您的特定情况下,您尝试从中获取对象的数组在索引 1 处没有对象

    一个简单的例子

    [0] => MyCoolObject
    [1] => MySecondObject
    [2] => ObjectTheThird
    

    您可以访问数组的索引 0、1、2,因为它们包含对象。如果您现在尝试访问索引 3,则会抛出 Out of bounds 异常,因为索引 3 不存在。

    【讨论】:

      猜你喜欢
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      • 2012-07-19
      • 2013-10-21
      • 2012-10-28
      相关资源
      最近更新 更多