Reusing NSPredicate to Replace a New Variable

Can I reuse NSPredicate to replace a new variable? My NSPredicate is pretty simple:

NSPredicate *userPredicate = [NSPredicate predicateWithFormat:@"id == %@",userID];

A user id is generated at runtime with a different value, right now for each user id I need to create an NSPredicate. Can I reuse my NSPredicate?

+5
source share
1 answer

Yes, you can do this with a variable:

NSPredicate *userPredicate = [NSPredicate predicateWithFormat:@"id == $user"];

Save this predicate somewhere, then do it when you need to actually start filtering the material:

NSDictionary *sub = [NSDictionary dictionaryWithObject:user forKey:@"user"];
NSPredicate *filter = [userPredicate predicateWithSubstitutionVariables:sub];

, ( ) .

+19

All Articles