【发布时间】:2014-06-25 11:14:27
【问题描述】:
我正在使用 Web 服务开发应用程序时遇到这样的错误,它是在我测试真实设备时发生的,但是当我运行视网膜 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:0]。 -
发布符号化的堆栈跟踪和相关代码。
-
您正在尝试访问一个空数组。您的 Web 服务可能没有停用任何内容,但您仍在尝试访问结果。
-
不要在评论中显示我们的代码。编辑您的问题以添加代码。
标签: ios objective-c json