2011-09-06 7 views
0

나는 실행할 때이 오류를주는 코드 블록이 있습니다. 그리고 나는 상대적으로 새로운데, 나는 문제를 해결할 수없는 것 같습니다.범위를 넘어 NSMutable 배열

오류 : 코어 애니메이션 : 무시 예외 : * - [NSMutableArray를 objectAtIndex :] 2011-09-06 12 : 31 : 06.094 ForceGauge [707 (266)]의 경계를 넘어 인덱스 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]; 
     } 
    } 
} 
+1

'[[NSMutableArray alloc] initWithObjects : 0, nil];'. NSArray는 원시 타입이 아닌 객체 만 포함 할 수 있습니다. '[[NSMutableArray alloc] initWithObjects : [NSNumber numberWithInt : 0], nil]; ' – albertamg

+0

@Bavarious는 여전히 같은 오류를줍니다. – ilaunchpad

+0

@ila 문제가 어딘가에있는 것 같은 인상을 받았기 때문에 내 대답이 삭제되었습니다. 내가 작성한 (그리고 albertamg가 주석으로 게시 한) 것은 여전히 ​​유효합니다. –

답변

2

여기에 여러 문제가 있습니다

1) NSArrays가 NSObjects 만 포함 할 수 있습니다. 코드에서

, 당신은 [[NSMutableArray alloc]initWithObjects:0, nil];를 사용하여 NSArray를 초기화되지만, 0은 NSObject의 원자 유형,하지 (기본적으로 0 전무 (전무와 NULL과 같은 값으로 해석 0으로 일반적으로 동일하다 당신은 각각 idvoid* 유형)

는 대신의 NSNumber에 0 값을 캡슐화 :

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

그런 [percentArray objectAtIndex:0]를 사용하여의 NSNumber를 검색하고 마지막으로 검색 다시 변환 SNumber는 NSNumberintValue 방법을 사용하여 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-1은 totalRows가 0 인 경우 까다로운 작업을 수행합니다 (사용자가 가진 예외를 고려하면 분명합니다). 이 0 일때 totalRows로서

NSUInteger이다 totalRows-1는 -1과 동일 할 않겠지보기 (NSUInteger)-1 (-1 부호없는 정수로 해석)은 0xFFFFFFFF 인, 또는 즉 최대 NSUInteger 유형의 가치!

itotalRows-1 값보다 항상 적은 이유는 (이 값이 아닌 -1 만로 을 0xFFFFFFFF = NSUInteger_MAX).

이 문제를 해결하려면 totalRows 변수를 NSInteger 값으로 캐스팅하거나 코드에 조건을 추가하여이 특수 사례를 개별적으로 처리하십시오.

+0

답변 됨 편집 됨 : 실제 이유가 실제로 발견되었습니다 – AliSoftware

+0

NSUInteger가 수정되었으며 오류가 제거되었습니다. 이제 다른 문제가 있습니다. 내가 아는 바로는 루프를 넘어서는 것이 아닙니다. – ilaunchpad

+0

다행 나는 그 때 당신의 문제를 해결했다. (좀 까다롭게 보았으므로!) – AliSoftware

0

for 루프의 시작 부분에 테스트 배열에 포함 된 객체 수를 확인하십시오.

NSUInteger를 피하고 배열 수를 저장하기 위해 간단한 int를 사용하면 도움이 될 수 있습니다.

문제가 해결되지 않으면 게시하십시오.

0

오류는 단순히 존재하지 않는 색인에서 개체를 검색하려고한다는 것을 의미합니다.특정 경우, 배열은 당신은 당신은 당신의 배열의 인덱스를 0, 1, 2에 액세스 할 수 있습니다

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

인덱스 1

쉬운 예에서 객체가없는에서 개체를 얻기 위해 노력하고 객체를 포함하기 때문입니다. 이제 인덱스 3에 액세스하려고하면 인덱스 3이 존재하지 않으므로 Out of bounds 예외가 발생합니다.

관련 문제