-1

I'm trying to filter with searchController. I am getting a crash (-[__NSArrayI length]: unrecognized selector sent to instance) at line NSRange productNameRange = NSMakeRange(0, [[_list valueForKey:@"name"] length]); in the following code:

-(void)updateFilteredContent:(NSString*)searchText {
    // Update the filtered array based on the search text and scope.
    [self.searchResults removeAllObjects];
    // Filter the array using NSPredicate
    NSLog(@"_categories are %@",_categories);
    for (NSDictionary *object in _categories) {
        NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
        NSRange productNameRange = NSMakeRange(0, [[_list valueForKey:@"name"] length]);
        NSRange foundRange = [[_list valueForKey:@"name"] rangeOfString:searchText options:searchOptions range:productNameRange];
        if (foundRange.length > 0) {
            [self.searchResults addObject:object];
        }
    }
}

I've tried adding an if statement on the length like if ([[object valueForKey:@"name"] length] >0){ but it still crashing.

an example of the log for _list is:

_list names are (
    "Honda",
    "Ford",
    Toyota,
user3411663
  • 1,041
  • 1
  • 9
  • 12

2 Answers2

0

your [_list valueForKey:@"name"] returns an NSarray, which should be an NSString to calculate length of it..

Tejvansh
  • 676
  • 4
  • 9
0

Please check with this condition and let me know if it still crashes!

if([_list allkeys] contains @"name"])
{
  NSString *tempStr = [_list valueForKey:@"name"];
}
if(tempStr)

NSRange productNameRange = NSMakeRange(0, [tempStr length]);