Reputation: 31
This is a pretty basic question here, and even though I think understand the basics of NSPredicate, I'm still a little stumped as to why I'm getting an error here (searchText is a pointer to a NSString object that's being passed into the method).
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.name contains[cd] %@", searchText];
_searchResults = [_personArray filteredArrayUsingPredicate:predicate];
I have a NSMutableArray of custom objects (e.g., Person objects), which have a name property. All I'm trying to do is use a NSPredicate to filter potential matches when the user begins typing in a UISearchBar. The IB component is straightforward enough and appears to function just fine. When I attempt to search and begin filtering however, the basic application crashes with the following error:
'NSInvalidArgumentException', reason: '-[xPerson isEqualToString:]: unrecognized selector sent to instance 0x8043340'
My understanding is the predicate will be passed to each object in the NSMutableArray, with the SELF keyword in the predicate referring to the respective object (and which, I believe, isn't even really necessary in this case), and the .name being able to use the synthesized property on the custom object. The error seems to suggest the -isEqualToString: call is being called on the custom object and not the object's property.
Any suggestions on what I'm doing wrong would be tremendously appreciated.
Upvotes: 3
Views: 3846
Reputation: 1
I had the same problem. The error wasn't in the precicate,but in the cell of the table view. I was setting the cell like this:
cell.textLabel.text = [self.searchResults objectAtIndex:indexPath.row];
I forgot to make the Object parse.Then I changed the code to this:
cell.textLabel.text = ((Product*)[self.searchResults objectAtIndex:indexPath.row]).name;
So,maybe your erros isn't in the NSPredicate too.
Upvotes: 0
Reputation: 23278
Try it like this,
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", searchText];
_searchResults = [_personArray filteredArrayUsingPredicate:predicate];
Upvotes: 5