0
votes

I want to filter an array with NSPredicate and I have some issues with it.

I have an NSArray named "recipes" where are saved my data.

Here my code :

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
    NSPredicate *resultPredicate = [NSPredicate
                                    predicateWithFormat:@"SELF contains[cd] %@",
                                    searchText];
    NSArray *searchRecipe = self.recipe[0];
    searchResults = [searchRecipe filteredArrayUsingPredicate:resultPredicate];
}

When i run this code, i get the following error :

2014-01-20 15:40:16.729 Recipes Editor[909:60b] -[filteredArrayUsingPredicate:]: unrecognized selector sent to instance 0x15666f80
2014-01-20 15:40:16.735 Recipes Editor[909:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[filteredArrayUsingPredicate:]: unrecognized selector sent to instance 0x15666f80'
*** First throw call stack:
(0x2f997f4b 0x39dd86af 0x2f99b8e7 0x2f99a1cb 0x2f8e94d8 0x12d01 0x12eeb 0x322fe3d9 0x3213f713 0x3213f6b3 0x3213f691 0x3212b11f 0x322fe149 0x321491cf 0x322bf261 0x322be807 0x322fdf77 0x322be525 0x302c67db 0x3214cdd9 0x322be453 0x32134a49 0x3037be9b 0x2f963183 0x2f962653 0x2f960e47 0x2f8cbc27 0x2f8cba0b 0x345f2283 0x3216f049 0x130c1 0x3a2e0ab7)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Why filteredArrayUsingPredicate can't filter my array ?

thx

3
self.recipe[0] does not seem to be an NSArray itself. But turn on debugging information and the problem will disclose itself. - user529758
Is searchBeacon defined? - revolver
Yes, searchBeacon is a mistake. But i've got the same error. - testoverblaireau

3 Answers

1
votes

I would use the filteredArrayUsingPredicate: method of NSArray like this:

    NSArray* filteredArray = [array filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {

    BOOL result = NO;

    // your evaluation code here

    return result;

}]];

The evaluatedObject is the current Object. You can easily check what class it is and validate everything you what. Just return YES if the evaluatedObject should stay after your evaluation or return NO if it should not included in the filtered array.

But be sure the array is an NSArray and not something else.

0
votes

Yes H2CO3 is said correctly your self.recipe[0] did not return you NSArray . You required debug your code either using breakpoint yeah following way

if([searchRecipe isKindOfClass:[NSArray class]]){
   if(searchRecipe.count>0){
    searchResults = [searchRecipe filteredArrayUsingPredicate:resultPredicate];
     }
    else{
       NSLog(@"array value %@", searchResults);
     }
  }
else{
 NSLog(@"Your return class isn't compatible");
}

This way you can avoid crashing thing.

0
votes

searchRecipe doesn't appear to be an NSArray. Add this debugging code:

NSArray *searchRecipe = self.recipe[0];
NSLog(@"searchRecipe type=%@", NSStringFromClass([searchRecipe class]));
searchResults = [searchRecipe filteredArrayUsingPredicate:resultPredicate];