1

I am programming the search bar logic in an iOS application. I have 1 NSArray "results", which has the entities returned from a NSFetchRequest, and an empty NSMutableArray "resultados" where I want to add the objects returned from the first array.

Here's the code:

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{

    [self.resultados removeAllObjects];

    NSFetchRequest *request = [NSFetchRequest alloc];
    request.predicate = [NSPredicate predicateWithFormat:@"numero contains [cd] %@",searchString];
    request.entity = [NSEntityDescription entityForName:@"Plaza" inManagedObjectContext:self.contexto];
    NSSortDescriptor *ordenPorNumero = [[NSSortDescriptor alloc]initWithKey:@"numero" ascending:YES];
    [request setSortDescriptors:[[NSArray alloc]initWithObjects:ordenPorNumero, nil]];

    NSError *error = nil;
    NSArray *results = [self.contexto executeFetchRequest:request error:&error];

    if(error){
        NSLog(@"Ha ocurrido un error: %@ %@",error,[error userInfo]);
        abort();
    }

    [self.resultados addObjectsFromArray:results];

    return YES;

}

Everytime this line is executed:

[self.resultados addObjectsFromArray:results];

I get the following exception:

2013-07-09 19:21:18.142 Actividad1[2347:c07] *** Terminating app due to uncaught exception NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 9 beyond bounds [0 .. 0]

I can't understand how can I go beyond any array bounds with the addObjectsFromArray method. In fact, if I replace this instruction for the following debug code which essentially does the same, I can see that I am adding all the objects, but always throw the exception when adding the last one, no matter how many objects the results return:

int num =0;
for(id i in results){
    Plaza *p = i;
    NSLog(@"%d - %@",num,p.numero);
    num++;
    [self.resultados addObject:i];
}

I've tried lots of variations, but I have no clue on what's going on.

Thanks in advance for your help.

Adrian P
  • 6,479
  • 4
  • 38
  • 55
  • What happens if you print out the `results` array? – Dan F Jul 09 '13 at 17:38
  • You have to call `[tableView reloadData]` after modifying the data source array `self.resultados`. – Martin R Jul 09 '13 at 17:42
  • Dan F, as you can see, I did that with the debug code, which prints the object "name" (called numero in this Entity) and then adds the object to the MutableArray. If I just remove the addObject instruction, it works fine and the Array "results" has the correct values. – mariogarranz Jul 09 '13 at 17:43
  • @MartinR, I'm not sure I can do that, since I don't have any reference to the UISearchBar tableView inside this method. None of the examples I've seen does it either :S – mariogarranz Jul 09 '13 at 17:49
  • Is the exception in fact being thrown at that line? If you turn on exception breakpoints, where does the code break? – Dan F Jul 09 '13 at 17:51
  • @Attanar: I am sorry, my above comment is wrong. Returning `YES` in `shouldReloadTableForSearchString` causes the search table view to be reloaded (as the method name implies :-) . Sorry for the confusion! – Martin R Jul 09 '13 at 17:58
  • @DanF When using breakpoints, I can get to this line: `0x1dffc: xorl %eax, %eax` That is the last one before it fails. I must say that when debugging it this way, I noticed I can get to the `return YES` line, and then fails. So I guess the error is not happening when adding objects, but because of something that happens only after objects are added? – mariogarranz Jul 09 '13 at 17:59
  • Turn on [exception breakpoints](http://developer.apple.com/library/ios/#recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html) to find the exact line of code that is throwing the uncaught exception, this should not be disassembly – Dan F Jul 09 '13 at 18:04
  • @DanF Thank you very much man! So far, I debugged the code by using my own breakpoints and saw nothing. Using your advice I came to see I was using the same `numberOfRowsInSection:(NSInteger)section` method for both my Controller tableView and the SearchBar tableView. I think I can fix that now. Thanks a lot again! :) – mariogarranz Jul 09 '13 at 18:12

0 Answers0