Qiu*_*ang 7 cocoa core-data nspredicate
我可以重用NSPredicate来替换新的变量吗?我的NSPredicate相当简单:
NSPredicate *userPredicate = [NSPredicate predicateWithFormat:@"id == %@",userID];
Run Code Online (Sandbox Code Playgroud)
userID是在运行时生成的,具有不同的值,现在对于我需要创建NSPredicate的每个userID.那么我可以重用我的NSPredicate吗?
Dav*_*ong 21
是的,您可以使用变量执行此操作:
NSPredicate *userPredicate = [NSPredicate predicateWithFormat:@"id == $user"];
Run Code Online (Sandbox Code Playgroud)
将该谓词保存在某个地方,然后在需要实际开始过滤内容时执行此操作:
NSDictionary *sub = [NSDictionary dictionaryWithObject:user forKey:@"user"];
NSPredicate *filter = [userPredicate predicateWithSubstitutionVariables:sub];
Run Code Online (Sandbox Code Playgroud)
这是重用谓词的一种更有效的方法,因为解析格式字符串(可能相当昂贵)只发生一次.
| 归档时间: |
|
| 查看次数: |
2422 次 |
| 最近记录: |