Reuse NSPredicate for new variable substitute

家住魔仙堡 提交于 2019-11-30 05:37:11

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!