错误:索引 1 超出范围 [0 .. 0]
Posted
技术标签:
【中文标题】错误:索引 1 超出范围 [0 .. 0]【英文标题】:Error: index 1 beyond bounds [0 .. 0] 【发布时间】:2013-01-19 15:52:32 【问题描述】:我是 Xcode 的菜鸟,我一直在使用 UITableViewController 显示此错误。
这是错误信息: * 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]” * 首先抛出调用栈: (0x1c91012 0x10cee7e 0x1c330b4 0x36d0 0xc58d5 0xc5b3d 0xacce83 0x1c450376 0x1c4f440 0x1c36f44 0x1c3624 0x1c36e1b 0x1b7e3 0x1beb668 0x1665c 0x2132 0x2065) libc++abi.dylib:终止调用抛出异常 (lldb)
这是我的代码:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
if (indexPath.row == 0)
NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/GetDetail.php?choice=row0"];
NSArray *arrayImagesNames = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];
arrayDataFromServer2 = [[NSMutableArray alloc]init];
NSEnumerator *enumForNames = [arrayImagesNames objectEnumerator];
id objName;
while ( objName = [enumForNames nextObject])
[arrayDataFromServer2 addObject:[NSDictionary dictionaryWithObjectsAndKeys:objName, @"name", nil]];
NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];
row1 = [nn intValue];
NSLog(@"%d", row1);
CompanyProfileViewController *profile = [self.storyboard instantiateViewControllerWithIdentifier:@"Profile"];
[self.navigationController pushViewController:profile animated:YES];
profile.profileid = row1;
NSLog(@"%d", profile.profileid);
if (indexPath.row == 1)
NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/GetDetail.php?choice=row1"];
NSArray *arrayImagesNames = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];
arrayDataFromServer2 = [[NSMutableArray alloc]init];
NSEnumerator *enumForNames = [arrayImagesNames objectEnumerator];
id objName;
while (objName = [enumForNames nextObject])
[arrayDataFromServer2 addObject:[NSDictionary dictionaryWithObjectsAndKeys:objName, @"name", nil]];
NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];
row1 = [nn intValue];
NSLog(@"%d", row1);
CompanyProfileViewController *profile = [self.storyboard instantiateViewControllerWithIdentifier:@"Profile"];
[self.navigationController pushViewController:profile animated:YES];
profile.profileid = row1;
NSLog(@"%d", profile.profileid);
if (indexPath.row == 2)
NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/GetDetail.php?choice=row2"];
NSArray *arrayImagesNames = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];
arrayDataFromServer2 = [[NSMutableArray alloc]init];
NSEnumerator *enumForNames = [arrayImagesNames objectEnumerator];
id objName;
while ( objName = [enumForNames nextObject])
[arrayDataFromServer2 addObject:[NSDictionary dictionaryWithObjectsAndKeys:objName, @"name", nil]];
NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];
row1 = [nn intValue];
NSLog(@"%d", row1);
CompanyProfileViewController *profile = [self.storyboard instantiateViewControllerWithIdentifier:@"Profile"];
[self.navigationController pushViewController:profile animated:YES];
profile.profileid = row1;
NSLog(@"%d", profile.profileid);
if (indexPath.row == 3)
NSString *strURL = [NSString stringWithFormat:@"http://localhost:8888/GetDetail.php?choice=row3"];
NSArray *arrayImagesNames = [[NSMutableArray alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];
arrayDataFromServer2 = [[NSMutableArray alloc]init];
NSEnumerator *enumForNames = [arrayImagesNames objectEnumerator];
id objName;
while ( objName = [enumForNames nextObject])
[arrayDataFromServer2 addObject:[NSDictionary dictionaryWithObjectsAndKeys:objName, @"name", nil]];
NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];
row1 = [nn intValue];
NSLog(@"%d", row1);
CompanyProfileViewController *profile = [self.storyboard instantiateViewControllerWithIdentifier:@"Profile"];
[self.navigationController pushViewController:profile animated:YES];
profile.profileid = row1;
NSLog(@"%d", profile.profileid);
当我单击第二个单元格 (index.row == 1) 时,会发生此错误。 我已经使用了断点并且错误就行了: "NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];"
请帮忙!
【问题讨论】:
尝试找出您的数据源为空的原因。学习阅读崩溃日志。 告诉我们确切的语句并尝试使用断点到达确切位置... @AnoopVaidya 我使用了断点,错误出现在 "NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];" 行当我点击 index.row == 1 【参考方案1】:斯威夫特 4
经过更多的时间,我得到了这个错误的爱? 当您使用情节提要中的 tableView 并在代码中实现时: 提示并比较: 在节数和 numberOfRowsInSection == 故事板和代码之间
override func numberOfSections(in tableView: UITableView) -> Int
// #warning Incomplete implementation, return the number of sections
return 3
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
// #warning Incomplete implementation, return the number of rows
return 1
【讨论】:
【参考方案2】:您有一个数组,其中包含 0 或 1 个元素。现在你试图从中提取第二个元素,[0] 是第一个,[1] 是第二个。
编辑:
因为您不确定数组何时会包含 1 个或多个对象。因此,您可以用作:
NSString *nn=nil;
if([arrayDataFromServer2 count]>1)
nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];
【讨论】:
我已编辑问题以更清楚地解释问题。请帮忙! 代码出现了另一个问题。当我使用此代码时: NSString *nn; if([arrayDataFromServer 计数]>1) nn = [[arrayDataFromServer objectAtIndex:indexPath.row] objectForKey:@"name"]; CompanyProfileViewController *profile = [self.storyboard instantiateViewControllerWithIdentifier:@"Profile"]; [self.navigationController pushViewController:profile Animation:YES]; profile.profileid = [nn intValue]; NSLog(@"%d", profile.profileid); ---- 不会更改 profile.profileid 值。您对此有什么建议吗? 不确定(因为这通常是一个 ios 逻辑,我不太喜欢),但您正在创建 CompyProfileViwConrlr 的新实例...以上是关于错误:索引 1 超出范围 [0 .. 0]的主要内容,如果未能解决你的问题,请参考以下文章