Search code examples
iphoneobjective-cxcodeios4nsarray

[NSArray subarrayWithRange:]: index 9779 beyond bounds


I am getting the error:

*** -[NSArray subarrayWithRange:]: index 9779 beyond bounds [0 .. 9776]'
***

and I am not sure how to fix it.

If you could tell me that would be great!

NSArray *keys = [NSArray arrayWithObjects:@"type", @"name", @"street", @"address1", @"address2", @"town", @"county", @"postcode", @"number", @"coffee club", @"latitude", @"longitude", nil];   
    for (int i = 0; i < [chunks count]; i += [keys count])
    {
        NSArray *subarray = [chunks subarrayWithRange:NSMakeRange(i, [keys count])];
        NSDictionary *dict = [[NSDictionary alloc] initWithObjects:subarray forKeys:keys];
        NSLog(@"%@", dict);
        // do something with dict

        [dict release];
    }

Solution

  • You don't say what chunks is in your code snippet. I guess the error is that you are accessing over the bounds of the chunks array.

    Maybe something like this would work better:

        for (int i = 0; i + [keys count] <= [chunks count]; i += [keys count])
    

    To elaborate a bit more. You are taking a sub array which starts at i and goes to [keys count] more elements, but there is no check that i + [keys count] doesn't go over the chunk array size. Perhaps that's causing a problem?