我正在使用网络服务开发应用程序我得到了这样的错误,当我测试真实设备时发生但是当我运行视网膜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;
}
答案 0 :(得分:1)
为了理解访问数组对象的概念,我在这里想出了一个例子,它清楚地解释了为什么当你在数组中的索引访问对象时它会崩溃。
我不会指出你犯错误的地方。理解这个概念,并根据这个改变你想要的代码[yourArray objectAtIndex:10]
。
非软件示例:
你有一个包(NSMutableArray)有10个标有(0,1,2 ... 9)的球,但是你怎么能找到第11个球。不可能!。
1.首先,你应该随手拿着手提包。([[NSMutableArray alloc] init]
)
2.然后尝试访问它。
3.你的问题是你正在寻找包内的第一个球(第0个标签)。 iOS说&#34;抱歉&#34;
//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]");
}
答案 1 :(得分:0)
您可以尝试:
id myObject = [myArray firstObject];
if(nil != myObject)
{
// Do something
}
但是没有向我们展示代码,我再也不能提出建议了。
答案 2 :(得分:0)
你可以用行数委托方法来做这个技巧。如果loadingcell数组是空的。然后单元格将加载空行..
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;
}
希望它可以帮助你..