I try understand what happens from some tutorials but unfortunately I am not found the answer.
I think that the problem may be that i didn’t have IF in case that I get nil in search result.
the error is:
‘NSRangeException’, reason: ‘* -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array’
I use that method:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
//removes previos search result
[[self searchResults]removeAllObjects];
for (Books *book in [self.fetchResultsController fetchedObjects])
{
if([self searchResults] != nil)
{
NSComparisonResult result = [book.title compare:searchText
options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)
range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame )
{
[self.searchResults addObject:book];
}
}
}
}
also can find my full project at the git:
https://github.com/dennis87/git_bookList
edit:
when I try put if like this one
if ([scope isEqualToString:@”All”] || [book.title isEqualToString:scope])
the app crush without let me press any thing..
The problem is that
numberOfSectionsInTableViewalways returns1for the search results table, even if there are no search results. The crash happens intitleForHeaderInSection:because the object at index 0 is accessed from an empty array.
So either you change
numberOfSectionsInTableViewto return0if the search results array is empty, or you changetitleForHeaderInSectionto return something different for the search results table.