开发者

[NSArray subarrayWithRange:]: index 9779 beyond bounds

开发者 https://www.devze.com 2023-02-15 17:30 出处:网络
I am getting the error: *** -[NSArray subarrayWithRange:]: index 9779 beyond bounds [0 .. 9776]\' *** and I am not sure how to fix it.

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 dic开发者_JAVA技巧t

        [dict release];
    }


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?


NSMakeRange does not work this way. The forst parameter is the starting index and the second is the length of the subarray. In your case, your code should look like this:

NSArray *subarray = [chunks subarrayWithRange:NSMakeRange(i, [keys count] - i)];

This is why you were getting the "index out of bounds" exception.

0

精彩评论

暂无评论...
验证码 换一张
取 消