Menu

  • 博客
  • 关于唯我&博客
  • 唯我DIY
  • 讨论区

Copyright © VIIIO.COM | Theme by Theme in Progress | 基于 WordPress

千里之行,始于足下唯我 - 梦想从此起航

iOS Cocoa过滤器NSPredicate的完全用法

2015年11月24日iOS Standard
Views: 1,229
注意:在predicateFormat中,” %@ ” 代表  ” ‘xxx’ “,不用再手动添加单引号
  1. Cocoa用NSPredicate描述查询的方式,原理类似于在数据库中进行查询  
  2. 计算谓词:  
  3. //基本的查询  
  4. NSPredicate *predicate;  
  5. predicate = [NSPredicate predicateWithFormat: @“name == ‘Herbie'”];  
  6.     BOOL match = [predicate evaluateWithObject: car];  
  7.     NSLog (@“%s”, (match) ? “YES” : “NO”);  
  8.   
  9. //在整个cars里面循环比较  
  10.     predicate = [NSPredicate predicateWithFormat: @“engine.horsepower > 150”];  
  11.     NSArray *cars = [garage cars];  
  12.     for (Car *car in [garage cars]) {  
  13.         if ([predicate evaluateWithObject: car]) {  
  14.             NSLog (@“%@”, car.name);  
  15.         }  
  16.     }  
  17.   
  18. //输出完整的信息  
  19.     predicate = [NSPredicate predicateWithFormat: @“engine.horsepower > 150”];  
  20.     NSArray *results;  
  21.     results = [cars filteredArrayUsingPredicate: predicate];  
  22.     NSLog (@“%@”, results);  
  23.   
  24. //含有变量的谓词  
  25.     NSPredicate *predicateTemplate = [NSPredicate predicateWithFormat:@“name == $NAME”];  
  26.     NSDictionary *varDict;  
  27.     varDict = [NSDictionary dictionaryWithObjectsAndKeys:  
  28.                @“Herbie”, @“NAME”, nil];  
  29.     predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];  
  30.     NSLog(@“SNORGLE: %@”, predicate);  
  31.     match = [predicate evaluateWithObject: car];  
  32.   NSLog (@“%s”, (match) ? “YES” : “NO”);  
  33. //注意不能使用$VARIABLE作为路径名,因为它值代表值  
  34.   
  35. //谓词字符窜还支持c语言中一些常用的运算符    
  36.     predicate = [NSPredicate predicateWithFormat:  
  37.                  @“(engine.horsepower > 50) AND (engine.horsepower < 200)”];  
  38.     results = [cars filteredArrayUsingPredicate: predicate];  
  39.     NSLog (@“oop %@”, results);  
  40.      
  41.     predicate = [NSPredicate predicateWithFormat: @“name < ‘Newton'”];  
  42.     results = [cars filteredArrayUsingPredicate: predicate];  
  43.     NSLog (@“%@”, [results valueForKey: @“name”]);  
  44.   
  45. //强大的数组运算符  
  46.     predicate = [NSPredicate predicateWithFormat:  
  47.                  @“engine.horsepower BETWEEN { 50, 200 }”];  
  48.     results = [cars filteredArrayUsingPredicate: predicate];  
  49.     NSLog (@“%@”, results);  
  50.      
  51.     NSArray *betweens = [NSArray arrayWithObjects:  
  52.                          [NSNumber numberWithInt: 50], [NSNumber numberWithInt: 200], nil];  
  53.     predicate = [NSPredicate predicateWithFormat: @“engine.horsepower BETWEEN %@”, betweens];  
  54.     results = [cars filteredArrayUsingPredicate: predicate];  
  55.     NSLog (@“%@”, results);  
  56.     predicateTemplate = [NSPredicate predicateWithFormat: @“engine.horsepower BETWEEN $POWERS”];  
  57.     varDict = [NSDictionary dictionaryWithObjectsAndKeys: betweens, @“POWERS”, nil];  
  58.     predicate = [predicateTemplate predicateWithSubstitutionVariables: varDict];  
  59.     results = [cars filteredArrayUsingPredicate: predicate];  
  60.     NSLog (@“%@”, results);  
  61.   
  62. //IN运算符  
  63.     predicate = [NSPredicate predicateWithFormat: @“name IN { ‘Herbie’, ‘Snugs’, ‘Badger’, ‘Flap’ }”];  
  64.     results = [cars filteredArrayUsingPredicate: predicate];  
  65.     NSLog (@“%@”, [results valueForKey: @“name”]);  
  66.     predicate = [NSPredicate predicateWithFormat: @“SELF.name IN { ‘Herbie’, ‘Snugs’, ‘Badger’, ‘Flap’ }”];  
  67.     results = [cars filteredArrayUsingPredicate: predicate];  
  68.     NSLog (@“%@”, [results valueForKey: @“name”]);  
  69.      
  70.     names = [cars valueForKey: @“name”];  
  71.     predicate = [NSPredicate predicateWithFormat: @“SELF IN { ‘Herbie’, ‘Snugs’, ‘Badger’, ‘Flap’ }”];  
  72.     results = [names filteredArrayUsingPredicate: predicate];//这里限制了SELF的范围  
  73.     NSLog (@“%@”, results);  
  74. //BEGINSWITH,ENDSWITH,CONTAINS  
  75.   
  76. //附加符号,[c],[d],[cd],c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分  
  77.     predicate = [NSPredicate predicateWithFormat: @“name BEGINSWITH ‘Bad'”];  
  78.     results = [cars filteredArrayUsingPredicate: predicate];  
  79.     NSLog (@“%@”, results);  
  80.      
  81.     predicate = [NSPredicate predicateWithFormat: @“name BEGINSWITH ‘HERB'”];  
  82.     results = [cars filteredArrayUsingPredicate: predicate];  
  83.     NSLog (@“%@”, results);  
  84.      
  85.     predicate = [NSPredicate predicateWithFormat: @“name BEGINSWITH[cd] ‘HERB'”];  
  86.     results = [cars filteredArrayUsingPredicate: predicate];  
  87.     NSLog (@“%@”, results);  
  88.   
  89. //LIKE运算符(通配符)  
  90.     predicate = [NSPredicate predicateWithFormat: @“name LIKE[cd] ‘*er*'”];  
  91.     results = [cars filteredArrayUsingPredicate: predicate];  
  92.     NSLog (@“%@”, results);  
  93.      
  94.     predicate = [NSPredicate predicateWithFormat: @“name LIKE[cd] ‘???er*'”];  
  95.     results = [cars filteredArrayUsingPredicate: predicate];  
  96.     NSLog (@“%@”, results);  

 

发表评论或回复 取消回复

邮箱地址不会被公开。

− 5 = 1

近期文章

  • OC UIWindow setRootViewController切换界面引发的内存问题
  • iOS证书、AppId、PP文件之间的关系
  • SVN服务器搭建、备份及多服务器同步方案(Windows)
  • [转]iOS多线程-各种线程锁的简单介绍
  • Mac 下Apache2 配置多虚拟主机

近期评论

  • NARYTHY288954NEYRTHYT发表在《ASP.NET整合Discuz PHP站 并实现用户同步》
  • nym402059flebno发表在《ASP.NET整合Discuz PHP站 并实现用户同步》
  • nem2182758krya发表在《ASP.NET整合Discuz PHP站 并实现用户同步》
  • aresgrb.se发表在《ASP.NET整合Discuz PHP站 并实现用户同步》
  • Vincenturbam发表在《ASP.NET整合Discuz PHP站 并实现用户同步》

分类目录

  • ASP.NET (15)
  • Git (2)
  • HTML (1)
  • iOS (31)
  • Javascript (7)
  • Oracle (8)
  • SQL (3)
  • SQLSERVER (2)
  • SVN (1)
  • 一行代码系列 (5)
  • 微信小程序 (1)
  • 正则表达式 (2)
  • 网站建设 (5)

文章归档

  • 2018年12月 (1)
  • 2018年4月 (1)
  • 2017年12月 (2)
  • 2017年7月 (3)
  • 2017年6月 (1)
  • 2017年4月 (1)
  • 2017年1月 (1)
  • 2016年12月 (3)
  • 2016年10月 (1)
  • 2016年7月 (1)
  • 2016年6月 (1)
  • 2016年5月 (3)
  • 2016年4月 (5)
  • 2016年3月 (4)
  • 2016年2月 (2)
  • 2016年1月 (3)
  • 2015年12月 (11)
  • 2015年11月 (7)
  • 2015年10月 (3)
  • 2015年9月 (1)
  • 2015年8月 (1)
  • 2015年7月 (1)
  • 2015年6月 (1)
  • 2015年5月 (1)
  • 2015年4月 (1)
  • 2014年7月 (1)
  • 2014年6月 (1)
  • 2014年5月 (2)
  • 2014年4月 (2)
  • 2014年3月 (2)
  • 2014年2月 (2)
2025年6月
一 二 三 四 五 六 日
« 12月    
 1
2345678
9101112131415
16171819202122
23242526272829
30