【问题标题】:Adding objects algorithm in Objective-C在 Objective-C 中添加对象算法
【发布时间】:2026-01-23 14:25:01
【问题描述】:

我有这个数组,它有五个介于 1 到 10 之间的随机数:

NSMutableArray *arrayOfNumbers = [NSMutableArray array];
    for (int x = 0; x < 5; x++)
    {
        [arrayOfNumbers addObject: [NSNumber numberWithInt: arc4random()%10]];
    }
    NSLog(@"%@",arrayOfNumbers);

我想把前两个和最后两个对象加在一起。如果它们加在一起,我想返回 2。如果它们不加在一起,我想检查第二个和第三个对象是否加起来最后两个对象。所以基本上如果我有数组 [2,3,9,4,1] 那么我想检查一下 2+3=4+1。在这种情况下,是的,所以我会返回 2。在这个数组中 [2,3,6,4,5] 2+3!=4+5 所以我们将继续检查 3+6=4+5 并且因为是我们返回 2。现在,如果没有两个对象相加时可以等于 [0,1,2,3,4],那么我们将返回 -1。

这是我到目前为止所做的:

    int lastValue = [arrayOfNumbers count];
    int secondlastValue = [arrayOfNumbers count] - 1;
    int firstValue = 0;
    int secondValue = 1;

    int i;
for (i = 0; i < [arrayOfNumbers count]; i++) {
    int one = [[arrayOfNumbers objectAtIndex:firstValue + i] integerValue];
    int two = [[arrayOfNumbers objectAtIndex:secondValue + i] integerValue];
    int secondtolast = [[arrayOfNumbers objectAtIndex:secondlastValue] integerValue];
    int last = [[arrayOfNumbers objectAtIndex:lastValue] integerValue];

    if (one + two == secondtolast + last) {
        NSLog(@"2: Because %i + %i = %i + %i",firstValue,secondValue,secondlastValue,lastValue);
        break;
    } else {
        NSLog(@"-1");
    }
}

但由于某种原因,它崩溃了...任何帮助将不胜感激。谢谢!

编辑:这是错误*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 5 beyond bounds [0 .. 4]'

编辑 2: 这些是我的 NSLog 的

if ((one + two) == totalOfLastValues) {
            NSLog(@"2: Because %@ + %@ (%i) = %@ + %@",arrayOfNumbers[i],arrayOfNumbers[i+1],one+two,[arrayOfNumbers objectAtIndex:[arrayOfNumbers count] - 2],[arrayOfNumbers lastObject]);
            break;
        } else {
            NSLog(@"-1: Because %@ + %@ (%i) != %@ + %@",arrayOfNumbers[i],arrayOfNumbers[i+1],one+two,[arrayOfNumbers objectAtIndex:[arrayOfNumbers count] - 2],[arrayOfNumbers lastObject]);
        }

【问题讨论】:

  • 您的代码看起来像是生成了超出范围的索引异常。为什么要遍历数组并将 i 添加到 firstvalue 和 secondvalue?您应该设置断点并单步执行您的代码,以便了解它的行为方式。

标签: ios iphone objective-c arrays


【解决方案1】:

它崩溃是因为 i 指向的最终值是数组中的最后一项。因此,您尝试查找的最后一件事是secondValue+i,它是数组中最后一项的后一项。这引发了异常。

同样,您的lastValuesecondLastValue 设置得太高了。数组中的第一项是项目0。因此,如果有两个项目,那么最后一个项目是项目1。如果有n 项,那么最后一项是n-1

所以:

        int lastValue = [arrayOfNumbers count] - 1;
        int secondlastValue = [arrayOfNumbers count] - 2;
        int firstValue = 0;
        int secondValue = 1;

        int i;
    for (i = 0; i < [arrayOfNumbers count]-1; i++) {
        int one = [[arrayOfNumbers objectAtIndex:firstValue + i] integerValue];
        int two = [[arrayOfNumbers objectAtIndex:secondValue + i] integerValue];
        int secondtolast = [[arrayOfNumbers objectAtIndex:secondlastValue] integerValue];
        int last = [[arrayOfNumbers objectAtIndex:lastValue] integerValue];

        if (one + two == secondtolast + last) {
            NSLog(@"2: Because %i + %i = %i + %i",firstValue,secondValue,secondlastValue,lastValue);
            break;
        } else {
            NSLog(@"-1");
        }
    }

编辑:您还想避免考虑最后两个数字加在一起是否等于最后两个数字加在一起。否则,您将始终返回 2。在循环内执行这部分算术也没有意义。所以第一个整洁的版本可能看起来像:

    for (int x = 0; x < 5; x++)
    {
        [arrayOfNumbers addObject: @(arc4random_uniform(10))];
    }
    NSLog(@"%@",arrayOfNumbers);

    int lastValue = [[arrayOfNumbers lastObject] integerValue];
    int secondLastValue = [[arrayOfNumbers objectAtIndex:[arrayOfNumbers count] - 2] integerValue];
    int totalOfLastValues = lastValue + secondLastValue;
    int firstValue = 0;
    int secondValue = 1;

        int i;
    for (i = 0; i < [arrayOfNumbers count]-3; i++) {
        int one = [[arrayOfNumbers objectAtIndex:firstValue + i] integerValue];
        int two = [[arrayOfNumbers objectAtIndex:secondValue + i] integerValue];

        if ((one + two) == totalOfLastValues) {
            NSLog(@"2: Because %i + %i (%i) = %i",firstValue+i,secondValue+i,one+two,totalOfLastValues);
            break;
        } else {
            NSLog(@"-1");
        }
    }

清理和防止对integerValue 的重复调用会导致:

    int lastValue = [[arrayOfNumbers lastObject] integerValue];
    int secondLastValue = [[arrayOfNumbers objectAtIndex:[arrayOfNumbers count] - 2] integerValue];
    int totalOfLastValues = lastValue + secondLastValue;

    int mostRecentValue  = [arrayOfNumbers[0] integerValue];
    for (int i = 1; i < [arrayOfNumbers count]-3; i++) {
        int one = mostRecentValue;
        int two = [arrayOfNumbers[i] integerValue];
        mostRecentValue = two;

        if ((one + two) == totalOfLastValues) {
            NSLog(@"2: Because %i + %i (%i) = %i",i,i+1,one+two,totalOfLastValues);
            break;
        } else {
            NSLog(@"-1");
        }
    }

【讨论】:

  • 这行得通,但是 nslog 返回数组 ( 5, 6, 6, 9, 6 ),然后返回 -1 两次,最后返回 2: Because 0 + 1 = 3 + 4
  • 在这种情况下,它添加了索引 2 和 3 以产生与索引 3 和 4 相同的结果 — 6 + 9 = 9 + 6。在我的编辑中,我限制了 i 的上限以避免这种情况。
  • hmm .. 数组是 ( 3, 6, 0, 6, 5 ) 但不知何故它仍然 NSLogged -1 三次然后 2: Because 3 + 4 (11) = 11 即使 3 和 4 甚至不在数组中
  • @ShalinShah 你的NSLog 记录索引,而不是值。我没有改变它,假设它是你想要的。
  • 我只是在学习 Objective-C,所以你介意告诉我如何 NSLog 值而不是索引吗?