Cocoa过滤器NSPredicate的完全用法

  1. Cocoa用nspredicate描述查询的方式,原理类似于在数据库中进行查询
  2. 计算谓词:
  3. //基本的查询
  4. nspredicate*predicate;
  5. predicate=[nspredicatepredicateWithFormat:@"name=='Herbie'"];
  6. BOOLmatch=[predicateevaluateWithObject:car];
  7. NSLog(@"%s",(match)?"YES":"NO");
  8. //在整个cars里面循环比较
  9. predicate=[nspredicatepredicateWithFormat:@"engine.horsepower>150"];
  10. NSArray*cars=[garagecars];
  11. for(Car*carin[garagecars]){
  12. if([predicateevaluateWithObject:car]){
  13. NSLog(@"%@",car.name);
  14. }
  15. }
  16. //输出完整的信息
  17. predicate=[nspredicatepredicateWithFormat:@"engine.horsepower>150"];
  18. NSArray*results;
  19. results=[carsfilteredArrayUsingPredicate:predicate];
  20. //含有变量的谓词
  21. nspredicate*predicateTemplate=[nspredicatepredicateWithFormat:@"name==$NAME"];
  22. NSDictionary*varDict;
  23. varDict=[NSDictionarydictionaryWithObjectsAndKeys:
  24. @"Herbie",@"NAME",nil];
  25. predicate=[predicateTemplatepredicateWithSubstitutionVariables:varDict];
  26. NSLog(@"SnorGLE:%@",predicate);
  27. match=[predicateevaluateWithObject:car];
  28. NSLog(@"%s",(match)?"YES":"NO");
  29. //注意不能使用$VARIABLE作为路径名,因为它值代表值
  30. //谓词字符窜还支持c语言中一些常用的运算符
  31. predicate=[nspredicatepredicateWithFormat:
  32. @"(engine.horsepower>50)AND(engine.horsepower<200)"];
  33. results=[carsfilteredArrayUsingPredicate:predicate];
  34. NSLog(@"oop%@",results);
  35. predicate=[nspredicatepredicateWithFormat:@"name<'Newton'"];
  36. NSLog(@"%@",[resultsvalueForKey:@"name"]);
  37. //强大的数组运算符
  38. @"engine.horsepowerBETWEEN{50,200}"];
  39. NSArray*betweens=[NSArrayarrayWithObjects:
  40. [NSNumbernumberWithInt:50],[NSNumbernumberWithInt:200],nil];
  41. predicate=[nspredicatepredicateWithFormat:@"engine.horsepowerBETWEEN%@",betweens];
  42. predicateTemplate=[nspredicatepredicateWithFormat:@"engine.horsepowerBETWEEN$POWERS"];
  43. varDict=[NSDictionarydictionaryWithObjectsAndKeys:betweens,@"POWERS",nil];
  44. predicate=[predicateTemplatepredicateWithSubstitutionVariables:varDict];
  45. //IN运算符
  46. predicate=[nspredicatepredicateWithFormat:@"nameIN{'Herbie','Snugs','Badger','Flap'}"];
  47. predicate=[nspredicatepredicateWithFormat:@"SELF.nameIN{'Herbie','Flap'}"];
  48. "name"]);
  49. names=[carsvalueForKey:@"name"];
  50. predicate=[nspredicatepredicateWithFormat:@"SELFIN{'Herbie',248)"> results=[namesfilteredArrayUsingPredicate:predicate];//这里限制了SELF的范围
  51. //BEGINSWITH,ENDSWITH,CONTAINS
  52. //附加符号,[c],[d],[cd],c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分
  53. predicate=[nspredicatepredicateWithFormat:@"nameBEGINSWITH'Bad'"];
  54. predicate=[nspredicatepredicateWithFormat:@"nameBEGINSWITH'HERB'"];
  55. predicate=[nspredicatepredicateWithFormat:@"nameBEGINSWITH[cd]'HERB'"];
  56. //LIKE运算符(通配符)
  57. predicate=[nspredicatepredicateWithFormat:@"nameLIKE[cd]'*er*'"];
  58. predicate=[nspredicatepredicateWithFormat:@"nameLIKE[cd]'???er*'"];

相关文章

正则替换html代码中img标签的src值在开发富文本信息在移动端...
正则表达式
AWK是一种处理文本文件的语言,是一个强大的文件分析工具。它...
正则表达式是特殊的字符序列,利用事先定义好的特定字符以及...
Python界一名小学生,热心分享编程学习。
收集整理每周优质开发者内容,包括、、等方面。每周五定期发...