0

When I do an NSLog of the contents of my NSMutableArray, it returns :

(
    hat,
    hat
)

So why is it that when I do an NSLog like so NSLog(@"%@", [pro.matches objectAtIndex:0]); it crashes with the error: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'

So strange

This is where I fill it:

[self.matches removeAllObjects];

         NSArray *JSONarray = [[NSArray alloc] initWithArray:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]];
         int i;

         for (i=0; i<[JSONarray count]; i++) {
             //[self.matches addObject:[JSONarray objectAtIndex:i]];
             [self.matches addObject:@"hat"];
         }
         //NSLog(@"boys with bo%@", [[matches objectAtIndex:1] objectForKey:@"host"]);
         block();
         //JSON and add to matches.
     }
     }];
    block(); 

and this is where i call it:

[pro refreshMatchesWithCallback:^ 
    {
        //[self.tableView reloadData];
        NSLog(@"the count of a lifetime is %@", [pro.matches objectAtIndex:0]);
    }];
michaela
  • 173
  • 1
  • 2
  • 13
  • 2
    This is not enough code to see what is wrong. Could you provide some more code? For example where you create/fill the array? – Manuel May 02 '12 at 14:38
  • 2
    When you print out the contents of the array as you posted, what is your `NSLog` call for that? And, as @dragon112 says, post more code so we can see your array being created. – Mike Z May 02 '12 at 14:41
  • @michaela in which class u do the NSlog plus tell me what is the senerio of the app r u using any sorta push pop, plus the thing i can think off is that the u must be setting the array to the pro object of the class, (but it all depands on the senerio of ur app) – WaaleedKhan May 02 '12 at 14:57

2 Answers2

1

When you first log the contents it has nothing in due to the way completion blocks work, try this:

[pro refreshMatchesWithCallback:^ 
{
    //[self.tableView reloadData];
    if(pro.matches.count > 0) {
       NSLog(@"the count of a lifetime is %@", [pro.matches objectAtIndex:0]);
    }
}];

Hope this Helps!

Sam

shoughton123
  • 4,255
  • 3
  • 21
  • 23
0
always prefer to use this approach

for(Object* obj in arra)
{
...

}

this will enter in loop if the counter is greater than 0. no check needed

Cheerz :P

Saad
  • 8,857
  • 2
  • 41
  • 51