PredicateWithSubstitutionVariables always throws an exception

I am trying to filter an array of objects using predicateWithSubstitutionVariables, this code is throwing an exception

NSPredicate *pSample = [NSPredicate predicateWithFormat:@"name CONTAINS [c] $variable"];
[pSample predicateWithSubstitutionVariables:@{@"variable":@"sample string"}];
NSLog(@"%@", [mArray filteredArrayUsingPredicate:pSample]);

      

An exception:

reason: 'Can't get value for 'variable' in bindings {
}.

      

Why can't I use this? (I am not looking for an alternative solution)

+3


source to share


1 answer


Change your code like

NSPredicate *pSample = [NSPredicate predicateWithFormat:@"name CONTAINS [c] $variable"];
NSPredicate *actualPredicate = [pSample predicateWithSubstitutionVariables:@{@"variable":@"sample string"}];
NSLog(@"%@", [mArray filteredArrayUsingPredicate: actualPredicate]);

      



The first predicate you pSample

create is the template used to create the actual predicate. predicateWithSubstitutionVariables

gives the actual predicate you want.

+4


source







All Articles