终止应用程序,由于未捕获的exception“NSRangeException”,原因:'*** – :索引0超出空数组的边界'
我正在开发使用Web服务的应用程序我得到这样的错误是这样的,当我testing真实的设备时,但是当我运行视网膜3.5模拟器,它完美的作品,请帮助我我已经尝试,但我不能解决这个问题。
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' *** First throw call stack: (0x30b59fd3 0x3b308ccf 0x30a9081b 0xeec19 0x334a8917 0x3344fc47 0x3344f49d 0x33375d79 0x32ff362b 0x32feee3b 0x32feeccd 0x32fee6df 0x32fee4ef 0x33379401 0x30b2525b 0x30b2472b 0x30b22f1f 0x30a8df0f 0x30a8dcf3 0x35992663 0x333d916d 0x100ec1 0x3b815ab7) libc++abi.dylib: terminating with uncaught exception of type NSException
代码:
-(void)loaddetails { NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://api.espn.com/v1/sports/football/news/headlines?apikey=th98vzm67pufc36ka42xxxmy"]]; [request setHTTPMethod:@"GET"]; [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"]; NSError *err; NSURLResponse *response; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; NSDictionary *jsonArray=[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&err]; NSArray *headlinetop=[jsonArray objectForKey:@"headlines"]; for (int i=0; i<[headlinetop count]; i++) { NSString *headstr=[[headlinetop objectAtIndex:i]objectForKey:@"headline"]; [loadingcell addObject:headstr]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; } UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10.0, 250, 26.0)]; title.backgroundColor = [UIColor clearColor]; title.textAlignment = NSTextAlignmentLeft; title.textColor=[UIColor blackColor]; title.font = [UIFont fontWithName:@"Arial Hebrew" size:15.0]; title.text=[loadingcell objectAtIndex:indexPath.row]; [cell.contentView addSubview:title]; if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) { cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; } return cell; }
为了理解访问数组对象的概念,在这里我想出了一个例子,它解释了为什么当你访问数组中的索引对象时,崩溃。
我不会指出你犯错误的地方。 理解这个概念,并基于这个改变代码,无论你有没有像[yourArray objectAtIndex:10]
。
非软件示例:
你有袋(NSMutableArray)有10个球从(0,1,2 … 9)标记,但你将如何找出第11个球。 不可能!。
首先,你应该随身携带包( [[NSMutableArray alloc] init]
)
2.然后尝试访问它。
你的问题在于你正在寻找袋子里的第一个球(第0个标签)。 iOS说“对不起”
//assume [array count] == 10 (0,1,2,3,4,5,...9) NSUInteger indexTobeAccessedInArray = 5;//6th object (0,1,2,3,4,5,...9) if (array && [array count]>0) { // array lives in memory and it has atleast one objects in it // 5 < 10 if (indexTobeAccessedInArray < [array count]) { //Yes NSLog(@"you can access this index:%i",indexTobeAccessedInArray); } else{ //No NSLog(@"Index %i beyond bounds for array",indexTobeAccessedInArray); } } else{ // There was no array in memory NSLog(@"sorry there is no array, you should create & initialize it first like \n NSMutableArray *array = [[NSMutableArray alloc]init]"); }
你可以尝试做:
id myObject = [myArray firstObject]; if(nil != myObject) { // Do something }
但没有向我们显示代码,我不能再提示。
你可以使用行数委托方法来做诀窍。如果加载单元格数组是空的。 然后单元格将加载空行
loadingcell=[[NSMutableArray alloc]init];//check that you initialized and allocated loadingcell
修改你的代码如下
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return loadingcell.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; } if(loadingcell.count>0) { UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(10, 10.0, 250, 26.0)]; title.backgroundColor = [UIColor clearColor]; title.textAlignment = NSTextAlignmentLeft; title.textColor=[UIColor blackColor]; title.font = [UIFont fontWithName:@"Arial Hebrew" size:15.0]; title.text=[loadingcell objectAtIndex:indexPath.row]; [cell.contentView addSubview:title]; if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) { cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; } } return cell; }
希望它可以帮助你..