Reuse NSPredicate for new variable substitute

后端 未结 1 958
清酒与你
清酒与你 2020-12-29 14:46

Can I reuse NSPredicate for new variable substitute? My NSPredicate is rather simple:

NSPredicate *userPredicate = [NSPredicate predicateWithFormat:@\"id ==          


        
相关标签:
1条回答
  • 2020-12-29 15:08

    Yep, you can do this with a variable:

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

    Save that predicate somewhere, then do this when you need to actually start filtering stuff:

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

    This is a much more efficient way to reuse a predicate, because parsing the format string (which can be pretty expensive) only happens once.

    0 讨论(0)
提交回复
热议问题