“ NSRangeException”错误,不确定为什么

用户名

我正在制作一个Objective C程序来生成两个随机数组,并检查它们是否具有相似的数字。我在标记的代码行上收到“ NSRangeException”,但我不确定为什么。这是我的代码:

// Array Comparator (Check Two Arrays for Similar Numbers)

@interface ArrayComparator: NSObject
{
    NSMutableArray *arrayOne;
    NSMutableArray *arrayTwo;
}

- (void) generateFirstArray;
- (void) generateSecondArray;
- (void) check;

@end

@implementation ArrayComparator

- (void) generateFirstArray
{
    arrayOne = [[NSMutableArray alloc] initWithCapacity: 50];

    for (NSUInteger n = 0; n < 50; n++)
    {
        [arrayOne addObject: @(arc4random_uniform(999) + 1)];
    }

    for (NSUInteger n = 0; n < 50; n++)
    {
        printf("%li, ", (long) [arrayOne[n] integerValue]);
    }
    printf("first array.\n\n");
}

- (void) generateSecondArray
{
    arrayTwo = [[NSMutableArray alloc] initWithCapacity: 50];

    for (NSUInteger n = 0; n < 50; n++)
    {
        [arrayTwo addObject: @(arc4random_uniform(999) + 1)];
    }

    for (NSUInteger n = 0; n < 50; n++)
    {
        printf("%li, ", (long) [arrayTwo[n] integerValue]);
    }
    printf("second array.\n\n");
}

- (void) check
{
    long similar = 0;

    for (NSUInteger n = 0; n < 50; n++)
    {
        for (NSUInteger m = 0; m < 50; n++)
        {
            if ([arrayOne[n] integerValue] == [arrayTwo[m] integerValue]) // This is where I get the error.
            {
                similar++;
            }
        }
    }
    printf("There are %li similar numbers between the two arrays!", similar);
}

@end

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        ArrayComparator *arrayComp = [[ArrayComparator alloc] init];
        [arrayComp generateFirstArray];
        [arrayComp generateSecondArray];
        [arrayComp check];
    } return 0;
}

任何帮助表示赞赏,谢谢。(请原谅我的无礼。)

马特
(NSUInteger m = 0; m < 50; n++)

你的意思是m++

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章