1、动态属性名

     假设你的代码如下

NSPredicate *p = [NSPredicate predicateWithFormat:@"name = %@", @"name1"]; 
显然代码没有任何问题,但是这个不是最好的写法我建议如下写法:
NSPredicate *preTemplate = [NSPredicate predicateWithFormat:@"name==$NAME"];
NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:
@"name1", @"NAME",nil];
NSPredicate *pre=[preTemplate predicateWithSubstitutionVariables: dic];

   这样看上去可能会让代码逻辑更清晰。

   2、当过滤条件字段都是动态的时候

    NSString *key = @"name";
    NSString *value = @"name1";
    NSPredicate *p = [NSPredicate predicateWithFormat:@"%@ = %@", key, value];

   然后当你执行到第三行的时候代码就会报错!
   逻辑上没错误啊!!!为什么会出错呢?
   NSPredicate要自动添加引号,所以最后得到的格式应该是@"‘name‘ = ‘name1‘"。明显不对。要做的就是:

NSPredicate *p = [NSPredicate predicateWithFormat:@"%K = %@", key, value]; 

总结:

          NSPredicate的简单的使用就介绍到这里,这里有些都是从网络上或者是其他的博客上看到的,以后有新的需求再进行完善。