Cocoa过滤器NSPredicate的完全用法

前端之家收集整理的这篇文章主要介绍了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*'"];
  59. 原文链接:https://www.f2er.com/regex/359919.html

猜你在找的正则表达式相关文章