am trying to add an array i get from NSURL into an array using for loop:
(int i = 0; i < 10; i++)
to make it 2 dimensional array. Keep getting error on NSRangeException. It can only insertObject or addObject at index[0] and when ‘i’ increase to 1, it throws an error. It can only mean that its not adding but overwriting so when it overwrites the second time and i = 1, it throws the error. How do i add more instead of overwriting the initialized 2 dimensional arrays?
- (void)qBlock{
for (int i = 0; i < 10; i++) {
NSURL *url = [NSURL URLWithString:@"http://somefiles.php"];
NSError *error;
NSStringEncoding encoding;
NSString *response = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
if (response) {
const char *convert = [response UTF8String];
NSString *responseString = [NSString stringWithUTF8String:convert];
NSMutableArray *sample = [responseString JSONValue];
block = [[NSMutableArray alloc]init];
[block insertObject:sample atIndex:i];
}
else {
UIAlertView *alert1 = [[UIAlertView alloc]initWithTitle:@"ALERT" message:@"Internet Connection cannot be established." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert1 show];
}
}
//Calling 10 blocks of codes
for (id obj in block) {
NSLog(@"%@",obj);
}
This:
Is replacing the entire block array with an empty array on each iteration through the for loop.
Move your block array initialization outside of the for loop.