1
votes

I am a rookie in Xcode and I have been using the UITableViewController which shows this error.

This is the error message: * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' * First throw call stack: (0x1c91012 0x10cee7e 0x1c330b4 0x36d0 0xc58d5 0xc5b3d 0xacce83 0x1c50376 0x1c4fe06 0x1c37a82 0x1c36f44 0x1c36e1b 0x1beb7e3 0x1beb668 0x1665c 0x2132 0x2065) libc++abi.dylib: terminate called throwing an exception (lldb)

This is my code:

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

When I click on the second cell (index.row == 1) this error would occur. I have used breakpoint and the error was on the line: "NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];"

Please Help!

2
Try to figure out why your data source is empty. Learn to read the crash logs. - dasdom
Tell us exactly which statement and try to use breakpoint to reach exact location... - Anoop Vaidya
@AnoopVaidya I have used breakpoint and the error was on line "NSString *nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];" when I click on the index.row == 1 - user1580957

2 Answers

3
votes

Swift 4

after more time i am get the love for this Error 😄 when you use the tableView from the storyboard and implementing in your code : hint that and compare : between the number of section and numberOfRowsInSection == storyboard and code

 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
    }
1
votes

You have an array and you have either 0 or 1 elements in it. Now you are trying to extract 2nd element from it, [0] is first, and [1] is second.

EDIT:

As you are not sure when the array will contain 1 or more objects. Therefore you can use as:

NSString *nn=nil;
if([arrayDataFromServer2 count]>1){    
    nn = [[arrayDataFromServer2 objectAtIndex:indexPath.row] objectForKey:@"name"];
}