I'm trying to configure my search bar in my app. I have an array with NSObject instances, and trying to use search option to find decent properties (of this NSObjects). Actually, i can find it. My problem is, i can do it only typing correct letters in UISearch field. When i try to type letter, that "name" property of my NSObject class did not contain, i get an error : index 0 beyond bounds for empty array. I perfectly understand whats going on - array is empty and i try to put out something from an empty array. But i already did check, if my array is empty, then why its error still occur? Please, take a look at code:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
self.searchResults = [NSArray new];
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText];
self.searchResults = [self.placeOfObjects filteredArrayUsingPredicate:resultPredicate];
if ([self.searchResults objectAtIndex:0] != nil){
_filteredName = ((placeHolder *)(self.searchResults[0])).name;
}
NSLog(@"name is %@", _filteredName );
}
self.placeOfObjects is an array that hold instances of NSObject class "place". It have several properties, name is one among them. It contain only text. How could i implement search without getting an error? Why it still trying to search in empty array when i type ([self.searchResults objectAtIndex:0] != nil)?
Any advice would be appreciated, thanks!