0

I've implemented a search bar (and display controller) in a uitableview using the code outlined below. When I type in the search text box I get the following sigabrt error:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<RestarauntEntity 0x7ae3f080> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'

I've re-reviewed the tutorial I've been following and quadruple checked the code however I cant find the cause of the error, can anyone help?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [self.searchResults count];
    } else {
        return _restaurantsInCity.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"RestaurantCell";
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    RestarauntEntity *currentRestaurant = nil;
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        currentRestaurant = [[self.searchResults objectAtIndex:indexPath.row]];
    } else {
        currentRestaurant = [self.restaurantsInCity objectAtIndex:indexPath.row];
    }

    NSString *decodedText1 = [currentRestaurant.title stringByReplacingOccurrencesOfString:@"&#8217;" withString:@"'"];
    NSString *decodedText = [decodedText1 stringByReplacingOccurrencesOfString:@"&#038;" withString:@"&"];
    cell.textLabel.text = decodedText;
    cell.textLabel.font = [UIFont fontWithName:@"avenir" size:16.0f];
    cell.textLabel.textColor=[UIColor blackColor];
    cell.detailTextLabel.text = currentRestaurant.city;
    cell.detailTextLabel.font = [UIFont fontWithName:@"avenir" size:12.0f];
    cell.detailTextLabel.textColor=[UIColor darkGrayColor];

    return cell;
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", searchText];
    searchResults = [_restaurantsInCity filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}
Sport
  • 8,570
  • 6
  • 46
  • 65
user1419810
  • 836
  • 1
  • 16
  • 29
  • 1
    Are you certain that your class `RestarauntEntity` has the property `name` ? – Max Chuquimia Jun 21 '15 at 05:32
  • please include the line where it crashes or the stack trace or anything more than just the error message. – luk2302 Jun 21 '15 at 05:49
  • Jugale, that was it, I feel so stupid, I needed to change "name" to the correct property "title"! That solved the crash but now the search results are blank, do you know why this could be? – user1419810 Jun 21 '15 at 06:00
  • No worries - as for results not loading, can you check if `tableView:cellForRowAtIndexPath:` is actually being called? Maybe you haven't assigned `self` as the `UISearchDisplayDelegate` so `self` isn't receiving the `searchDisplayController:shouldReloadTableForSearchString:` call? – Max Chuquimia Jun 21 '15 at 07:39
  • Also, it's worth noting that `searchDisplayController:shouldReloadTableForSearchString:` is deprecated in iOS8 – Max Chuquimia Jun 21 '15 at 07:39
  • possible duplicate of [What does this mean? "'NSUnknownKeyException', reason: ... This class is not key value coding-compliant for the key X"](http://stackoverflow.com/questions/3088059/what-does-this-mean-nsunknownkeyexception-reason-this-class-is-not-key) – jtbandes Aug 03 '15 at 06:19

0 Answers0