ios - 应用程序由于未捕获的异常“NSRangeException”而终止

标签 ios objective-c arrays uitableview

我正在尝试以横向模式在表上显示数组的值,但是出现以下错误,这会使应用程序崩溃:

由于未捕获的异常'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];
  });
}

最佳答案

9223372036854775807-1的无符号转换,即NSNotFound。根据错误消息,您正在调用-[NSString substringToIndex:],但搜索结果未找到您要查找的内容。

该部分代码未显示,因此请在任何您认为可以拉开字符串但未正确检查是否可以实际使用的位置上查找。尝试在连接了调试器的情况下运行,它将告诉您代码的哪一行引发所报告的异常。您可以从那里向后工作。

关于ios - 应用程序由于未捕获的异常“NSRangeException”而终止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47892812/

相关文章:

iphone - 如何获取接口(interface)中定义的对象数组的计数?

iphone - 需要优化 SQLite 加载到 TableView 中

ios - 滚动时页脚覆盖在 tableview 单元格内容之上

javascript - 扩展 Angular 服务/工厂以获取日期数组和整数数组,并创建带有键值对的数组

PHP 在对大数组使用 for 循环时卡住

ios - 将 sizeWithFont 方法转换为 Swift

ios - iOS 中返回的运营商名称是网络运营商名称还是 sim 运营商名称?

ios - 无法确定 iPod 中的位置?

iphone - 如何将文本的第一个字母大写

python - 使用 numpy 在数组中查找 N 个并发的索引