NSPredicate类,指定过滤器的条件---董鑫
2015-09-18 17:05
253 查看
/* 比较和逻辑运算符 就像前面的例子中使用了==操作符,NSPredicate还支持>, >=, <, <=, !=, <>,还支持AND, OR, NOT(或写成C语言样式&&, ||, ! ),其中AND,OR,NOT还是不区分大小写的 */ NSArray *array = [NSArray array]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age <= 28"]; for (Student *student in array) { //标示指定的对象是否满足谓语条件 if ([predicate evaluateWithObject:student]) { NSLog(@"person name : %@",student.name); } } //返回一个符合谓语条件的数组 NSArray *newArray = [array filteredArrayUsingPredicate:predicate]; for (Student *student in newArray) { NSLog(@"person name : %@",student.name); } /* 关键字 IN,BEGINSWITH,ENDSWITH,CONTAINS,LIKE,大小写都可以.添加的限制性字符串一定要添加'' */ Student *student = [[Student alloc]init]; NSPredicate *preIN = [NSPredicate predicateWithFormat:@"student.name IN {'rose','bruse'}"]; NSArray *student1 = [array filteredArrayUsingPredicate:preIN]; //以**开始beginswith NSPredicate *preBeginsWith = [NSPredicate predicateWithFormat:@"student.name beginswith '滚犊子'"]; NSArray *student2 = [array filteredArrayUsingPredicate:preBeginsWith]; //以**结尾endswith NSPredicate *preEndsWith = [NSPredicate predicateWithFormat:@"student.name endswith '滚犊子'"]; NSArray *student3 = [array filteredArrayUsingPredicate:preEndsWith]; //包含contains NSPredicate *preContains = [NSPredicate predicateWithFormat:@"student.name contains '滚犊子'"]; NSArray *student4 = [array filteredArrayUsingPredicate:preContains]; //模糊查询like NSPredicate *preLike = [NSPredicate predicateWithFormat:@"student.name like '*%@*'",@"滚犊子"]; NSArray *student5 = [array filteredArrayUsingPredicate:preLike];
相关文章推荐
- jQuery插件Flot实战Demo
- Linux chattr和lsattr
- 豆瓣爬虫
- TFS erasure code实现方案
- ul li高度在ie6、ie7、ie8、FF下兼容问题解决
- 第一次开源项目贡献
- jenkins系列教程
- MySQL学习5_CentOS下Mysql数据库(不使用yum命令)的安装与配置
- SEAndroid中sepolicy/adbd.te策略文件分析
- Unity脚本中使用NGUI的预先准备
- 多进程和多线程的优缺点
- iOS适配器(Adapter)模式
- 二叉树中节点删除
- Android数据适配器(Adapter)优化:高效ViewHolder
- MySQL问题,设置为不允许为空的字段却可以为空
- 关于res-auto
- lintcode——排序列表转换为二分查找树(链表,二叉排序树)
- python thrift 服务端与客户端使用
- 黑马程序员_java面向对象_多态和异常
- Android加载通话记录流程分析