2012-05-02 190 views
0

當我做了我的NSMutableArray的內容的NSLog的,它返回:ObjectAtIndex會導致應用程序崩潰

(
    hat, 
    hat 
) 

那麼,爲什麼,當我做一個的NSLog像這樣NSLog(@"%@", [pro.matches objectAtIndex:0]);它與錯誤崩潰:*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

太奇怪了

這是我填滿它:

[self.matches removeAllObjects]; 

     NSArray *JSONarray = [[NSArray alloc] initWithArray:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]]; 
     int i; 

     for (i=0; i<[JSONarray count]; i++) { 
      //[self.matches addObject:[JSONarray objectAtIndex:i]]; 
      [self.matches addObject:@"hat"]; 
     } 
     //NSLog(@"boys with bo%@", [[matches objectAtIndex:1] objectForKey:@"host"]); 
     block(); 
     //JSON and add to matches. 
    } 
    }]; 
    block(); 

而這正是我稱之爲:

[pro refreshMatchesWithCallback:^ 
    { 
     //[self.tableView reloadData]; 
     NSLog(@"the count of a lifetime is %@", [pro.matches objectAtIndex:0]); 
    }]; 
+2

這是不夠的代碼,看看什麼是錯的。你能提供更多的代碼嗎?例如,你創建/填充數組? – Manuel

+2

當你打印出你發佈的數組內容時,你的'NSLog'調用了什麼?而且,正如@ dragon112所說,發佈更多代碼,以便我們可以看到您的數組正在創建。 –

+0

@michaela在哪個類中做NSlog加上告訴我什麼是應用程序ru的senerio使用任何sorta push pop,加上我能想到的事情是,你必須將數組設置爲類的專業對象,(但這一切都取決於你的應用程序的senerio) – WaaleedKhan

回答

1

首次登錄時它沒有任何因方式完成工作的塊的內容,試試這個:

[pro refreshMatchesWithCallback:^ 
{ 
    //[self.tableView reloadData]; 
    if(pro.matches.count > 0) { 
     NSLog(@"the count of a lifetime is %@", [pro.matches objectAtIndex:0]); 
    } 
}]; 

希望這有助於!

山姆

0
always prefer to use this approach 

for(Object* obj in arra) 
{ 
... 

} 

計數器是否大於0無校驗需要

Cheerz這將在循環進入:P