0

I am trying to set my predicateWithBlock to receive the text of my search bar and filter all the results of my NSDictionary with the objects that have these texts.   I have not used it then do not know what I'm doing wrong.

When I type something in the search bar I get this error "NSInvalidArgumentException ', reason:' - [__ NSDictionaryI hasPrefix]: unrecognized selector sent to instance 0x79866530"

Below is my code:

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

    if (searchText.length == 0) {
        self.isFilltered = NO;
    }else{

        self.isFilltered = YES;


        NSPredicate *evenNumbers = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
            return [evaluatedObject hasPrefix:self.contactSearchBar.text];
        }];

        NSArray *filteredArray = [self.fakeData filteredArrayUsingPredicate:evenNumbers];



        self.tableData = [@[@{@"rows": filteredArray}] mutableCopy];
        [self.dataTableView reloadData];
    }

}
  • How do you know you're doing _anything_ "wrong"? – matt May 21 '15 at 22:13
  • Because this not working. –  May 21 '15 at 22:21
  • But what is "this" and "not working"? What do you expect to happen, and what does happen? And how do you know it has anything to do with your use of `predicateWithBlock`? Have you examined `filteredArray` to see `fakeData` is being filtered correctly? – matt May 21 '15 at 22:23
  • I want it to return all items NSDictionary containing the text the search bar. The search bar is the self.contactSearchBar.text. When I type something I get this error "NSInvalidArgumentException ', reason:' - [__ NSDictionaryI hasPrefix]: unrecognized selector sent to instance 0x79866530" –  May 21 '15 at 22:27
  • Very good. But you need to say that _as part of your question_. Do you see? – matt May 21 '15 at 22:28
  • Meanwhile I'm now able to make an initial guess at an answer! – matt May 21 '15 at 22:30
  • possible duplicate of [How can I debug 'unrecognized selector sent to instance' error](http://stackoverflow.com/questions/25853947/how-can-i-debug-unrecognized-selector-sent-to-instance-error) – Hot Licks May 22 '15 at 01:05

1 Answers1

0

You need to ask yourself what fakeData is. My guess is that it is an NSArray of NSDictionary. If my guess is right, this would explain the error you're getting; on each pass through the predicate block, evaluatedObject is an NSDictionary - and you cannot say hasPrefix to an NSDictionary, as you would then be attempting to do.

If your NSDictionary has a @"name" key, then you would say:

return [[evaluatedObject objectForKey:@"name"] hasPrefix:self.contactSearchBar.text];
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • ok. What do I but I can use instead of my hasPrefix. I have a NSArray of NSDictionary same. I need to get the key value "name" but I do not know how. –  May 21 '15 at 22:34
  • If it is an NSDictionary then that would be `objectForKey:@"name"`. And now, if _that_ is an NSString, you can ask whether _that_ has a prefix! – matt May 21 '15 at 22:35
  • Updated my answer to show what that code might look like. This may not solve your whole problem, but it might get you past this crash! – matt May 21 '15 at 22:36