由于未捕获的异常“NSRangeException”而导致应用程序终止
Posted
技术标签:
【中文标题】由于未捕获的异常“NSRangeException”而导致应用程序终止【英文标题】:App Terminating due to uncaught exception 'NSRangeException' 【发布时间】:2017-12-19 18:06:06 【问题描述】:我正在尝试以横向模式在表格上显示数组的值,但是我收到以下错误并导致应用程序崩溃:
由于未捕获的异常 'NSRangeException' 导致应用程序终止,原因:'* -[__NSCFString substringToIndex:]:索引 9223372036854775807 越界;字符串长度 10'**。
这是我的代码:
- (void)viewWillAppear:(BOOL)animated
[super viewWillAppear:YES];
NSLog(@"Orientation Current: %ld", (long)[[UIApplication sharedApplication] statusBarOrientation]);
if([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)
NSLog(@"Here found");
[self forceToOrientation:UIInterfaceOrientationLandscapeLeft];
- (void)viewDidLayoutSubviews
[super viewDidLayoutSubviews];
[self.completedSubscriptionsTableView setNeedsLayout];
[self.completedSubscriptionsTableView layoutIfNeeded];
- (void)viewWillLayoutSubviews
[super viewWillLayoutSubviews];
[self.completedSubscriptionsTableView setNeedsLayout];
[self.completedSubscriptionsTableView layoutIfNeeded];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
return 1;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
return [self.subscriberArray count];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell;
PaymentHistoryCellTableViewCell *paymentHistoryCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
[paymentHistoryCell setSelectionStyle:UITableViewCellSelectionStyleNone];
PaymentHistory *currentPayment = [self.subscriberArray objectAtIndex:indexPath.row];
NSLog(@"Current Payment: %@", currentPayment);
[paymentHistoryCell.lblTransactionNo setText:[NSString stringWithFormat:@"\t%@", [[self.subscriberArray objectAtIndex:indexPath.row] valueForKey:@"Name"]]];
[paymentHistoryCell.lblAmount setText:[NSString stringWithFormat:@"\t%@", [[self.subscriberArray objectAtIndex:indexPath.row] valueForKey:@"address"]]];
if (indexPath.row % 2 == 0)
[paymentHistoryCell.contentView setBackgroundColor:[UIColor colorWithRed:232/255.0 green:232/255.0 blue:232/255.0 alpha:1.0]];
else
[paymentHistoryCell.contentView setBackgroundColor:[UIColor whiteColor]];
cell = paymentHistoryCell;
[cell setNeedsLayout];
[cell layoutIfNeeded];
[cell layoutSubviews];
[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];
return cell;
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
int space = 0;
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40.0)];
[headerView setBackgroundColor:[UIColor colorWithRed:210/255.0 green:210/255.0 blue:210/255.0 alpha:1.0]];
CGFloat viewWidth = (self.view.frame.size.width / [self.headerItems count]);
NSLog(@"viewWidth: %f", viewWidth);
for(int i = 0; i < [self.headerItems count]; i++)
UILabel *headerLbl = [[UILabel alloc] initWithFrame:CGRectMake((space * i) + (viewWidth * i), headerView.frame.origin.y, viewWidth, headerView.frame.size.height)];
[headerLbl.layer setBorderWidth:1.0];
[headerLbl setTextAlignment:NSTextAlignmentCenter];
[headerLbl setText:[self.headerItems objectAtIndex:i]];
[headerLbl setFont:[UIFont boldSystemFontOfSize:15.0]];
[headerLbl.layer setBorderColor:[[UIColor blackColor] CGColor]];
[headerView addSubview:headerLbl];
return headerView;
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
return 40.0;
#pragma mark - Orientation Methods
- (BOOL)shouldAutorotate
return YES;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
return 40.0;
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
return UIInterfaceOrientationMaskLandscapeLeft;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
return UIInterfaceOrientationLandscapeLeft;
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
if (toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
[self.navigationController popViewControllerAnimated:YES];
else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
[self forceToOrientation:toInterfaceOrientation];
- (void)forceToOrientation:(UIInterfaceOrientation)toInterfaceOrientation
NSLog(@"Forcing orientation to %ld",(long)toInterfaceOrientation);
[[UIDevice currentDevice] setValue:
[NSNumber numberWithInteger: UIInterfaceOrientationLandscapeLeft]
forKey:@"orientation"];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
- (void)setCompletedInstallations:(NSMutableArray *)compleInstalltions
self.subscriberArray = [[NSMutableArray alloc]init];
dispatch_async(dispatch_get_main_queue(), ^
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"SubmittedAt" ascending:NO];
self.subscriberArray = [NSMutableArray arrayWithArray: [compleInstalltions sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]];
NSLog(@" These are the completed subscriptions -> %@",_subscriberArray);
[self.completedSubscriptionsTableView reloadData];
);
【问题讨论】:
检查您的数组,错误提示 ex-:您的数组有 10index,但您尝试访问第 11 个。检查您是否有任何此类情况。 告诉我们在哪条线上被触发可能会有所帮助。 唯一一次访问数组是 numberOfRowsInSection、cellForRowAtIndexPath 和 setCompletedInstallations 您显示的代码不应产生错误。你只用cellForRow
“玩”字符串,你用withFormat:
,所以不在那里。但显然[[self.subscriberArray objectAtIndex:indexPath.row] valueForKey:@"zzz"]
这通常是使用valueForKey:
的坏习惯,而您已经获得了PaymentHistory
。为什么不做[paymentHistoryCell.lblTransactionNo setText:[currentPayment Name]]
或类似的东西(或者你可以使用valueForKey:
而不是在自定义单元格上添加“\t”效果?
不相关,但您是否详细了解cellForRow
中的所有后续调用setNeedsLayout
layoutIfNeeded
、layoutSubviews
等正在做什么,或者您的意思是不要伤害,把它们都放进去?在任何单元格中调用时,某些方法可能会很昂贵。
【参考方案1】:
9223372036854775807
是-1
的无符号转换,即NSNotFound
。根据错误消息,您正在调用 -[NSString substringToIndex:]
,但搜索结果未找到您要查找的内容。
这部分代码未显示,因此请寻找您认为可以拆分字符串但未正确检查您是否确实可以拆分的任何地方。尝试使用附加的调试器运行,它会告诉您代码的哪一行引发了报告的异常。您可以从那里向后工作。
【讨论】:
在我的代码中我有这两行我没有添加,scheduledDate = [schedDate substringToIndex:rangeOfSubstring.location];和 completedDate = [complDate substringToIndex:rangeOfSubstringg.location]; 如果你检查rangeOfSubstring
——或者NSLog
或者设置一个断点——你应该看到rangeOfSubstring.location
是-1
,这意味着你调用的计算它的方法不能找到你要求它寻找的东西。
非常感谢,您的回答很清楚,您的建议也很到位。我发现了我的错误并修复了它。非常感谢【参考方案2】:
未找到您尝试使用的索引,从而导致崩溃。检查您传递给方法 [NSString substringToIndex:]
的值【讨论】:
以上是关于由于未捕获的异常“NSRangeException”而导致应用程序终止的主要内容,如果未能解决你的问题,请参考以下文章
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围”
由于未捕获的异常“NSRangeException”而终止应用程序,原因:-[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围
由于未捕获的异常“NSRangeException”而终止应用程序,原因:“-[__NSCFArray objectAtIndex:]: index (14) beyond bounds (14)”
由于未捕获的异常“NSRangeException”而终止应用程序,原因:-[__NSArrayM objectAtIndex:]:索引 0 超出空数组的范围-2
*** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0
*** 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds fo