2014-03-26 36 views
0

我正在做一個Objective C程序來生成兩個隨機數組並檢查它們的相似數字。我在標記的代碼行上得到了「NSRangeException」,但我不知道爲什麼。這裏是我的代碼:「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; 
} 

任何幫助表示讚賞,謝謝。 (請原諒我的noobishness。)

回答

2
(NSUInteger m = 0; m < 50; n++) 

您的意思是m++

+0

哈哈,謝謝!我現在覺得很愚蠢。 XD – AmiableNebula

相關問題