2011-09-06 75 views
0

我有一段代碼,當執行時會給我這個錯誤。而且我相對較新,似乎無法解決問題。NSMutable Array超越界限

錯誤: 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]; 
     } 
    } 
} 
+1

'[[NSMutableArray alloc] initWithObjects:0,nil];'。 NSArray只能包含對象,而不能包含原始類型,所以需要將它們包裝起來。 '[[NSMutableArray alloc] initWithObjects:[NSNumber numberWithInt:0],nil];' – albertamg

+0

@Bavarious仍然給出同樣的錯誤。 – ilaunchpad

+0

@ila我印象這個問題是在別的地方,這就是爲什麼我刪除了我的答案。不過,我寫的東西(和albertamg發表的評論)仍然有效。 – 2011-09-06 17:50:25

回答

2

有多種問題在這裏:

1)NSArrays只能包含NSObjects。

在代碼中,使用的是[[NSMutableArray alloc]initWithObjects:0, nil];初始化您的NSArray,但0是一個原子類型,而不是一個NSObject (和基本上0是相同的值零(零和NULL通常等於0,解釋爲在idvoid*類型,分別)

你必須在一個NSNumber封裝的0值,而不是:

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

使用[percentArray objectAtIndex:0]然後檢索的NSNumber並最終轉換回取出N個SNumber爲int使用NSNumberintValue方法:

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(這是很明顯的情況考慮你有例外)。

作爲totalRowsNSUInteger,當它等於0時,totalRows-1將不等於-1,但爲... (NSUInteger)-1(-1解釋爲無符號整數),這是爲0xFFFFFFFF,或即最高NSUInteger類型的值!

這就是爲什麼i總是比這個數值totalRows-1少(因爲這個值是不是-1,但爲0xFFFFFFFF = NSUInteger_MAX)。

要解決此問題,請將您的totalRows變量強制轉換爲NSInteger值,或者在代碼中添加一個條件以分別處理該特殊情況。

+0

回答編輯:實際找到真正的原因 – AliSoftware

+0

我修復了NSUInteger,它擺脫了錯誤。現在我還有其他問題,我知道現在它不會超越循環。 – ilaunchpad

+0

很高興我解決你的問題,然後(因爲它有點棘手,看到!):) – AliSoftware

0

請檢查for循環開始時測試數組包含的對象數量。

其他可以幫助的事情是避免使用NSUInteger並使用簡單的int來存儲數組數。

如果這不起作用,請發佈。

0

錯誤僅僅意味着您試圖在不存在的索引處檢索對象。在特定情況下,數組你試圖獲取對象從索引1

一個簡單的例子

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

您可以訪問索引0,1,2的數組沒有對象因爲它們包含對象。如果您現在嘗試訪問索引3,則會拋出Out of bounds異常,因爲索引3不存在。