2017-12-19 6 views
1

Ich versuche, die Werte eines Arrays auf einem Tisch im Querformat angezeigt werden, aber ich bekomme die folgende Fehlermeldung und stürzt die App:App Terminating aufgrund nicht abgefangene Ausnahme ‚NSRangeException‘

App beenden wegen Uncaught-Ausnahme 'NSRangeException', Grund: '* - [__ NSCFString substringToIndex:]: Index 9223372036854775807 außerhalb der Grenzen; Saitenlänge 10 '**.

Dies ist mein Code:

- (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]; 
    }); 
} 
+0

überprüfen Sie Ihr Array, Fehler sagt für ex: Ihr Array haben 10index, aber Sie haben versucht, 11. zuzugreifen. Überprüfen Sie, ob Sie einen solchen Fall haben. –

+0

Erzähl uns, auf welcher Linie ausgelöst wird, könnte helfen. – Tommy

+0

die einzige Zeit, die ich auf das Array zugreifen, ist numberOfRowsInSection, cellForRowAtIndexPath und setCompletedInstallations – DvixExtract

Antwort

3

9223372036854775807 ist die unsigned Besetzung von -1, die NSNotFound ist. Nach der Fehlermeldung rufen Sie -[NSString substringToIndex:] mit dem Ergebnis einer Suche, die nicht gefunden, was Sie gesucht haben.

Dieser Codeabschnitt wird nicht angezeigt. Suchen Sie also nach einem Ort, von dem Sie glauben, dass Sie eine Zeichenfolge auseinander ziehen können, aber nicht genau prüfen, ob Sie das tatsächlich können. Versuchen Sie, mit dem angehängten Debugger zu laufen, und es wird Ihnen sagen, welche Zeile Ihres Codes die gemeldete Ausnahme auslöst. Sie können von dort rückwärts arbeiten.

+0

in meinem Code erzwinge Ich habe diese zwei Zeilen, die ich nicht hinzugefügt habe, scheduledDate = [schedDate substringToIndex: rangeOfSubstring.location]; und completedDate = [complDate substringToIndex: rangeOfSubstringg.location]; – DvixExtract

+0

Wenn Sie 'rangeOfSubstring' überprüfen - entweder' NSLog' it oder einen Haltepunkt setzen - sollten Sie sehen, dass 'rangeOfSubstring.location'' -1 ist ', was bedeutet, dass die Methode, die Sie aufgerufen haben, um es zu berechnen, das Objekt, nach dem Sie suchen, nicht finden kann. – Tommy

+0

ok lass mich das versuchen – DvixExtract

1

Der Index, den Sie verwenden möchten, wird nicht gefunden, was zum Absturz führt. Überprüfen Sie den Wert, den Sie an Methode übergeben [NSString substringToIndex:]

Verwandte Themen