NSPredicate 的使用
NSPredicate是什么?
NSPredicate 是预测的意思 但是我们常翻译成谓词
它可以干什么?
使用NSPredicate可以定义模糊查询条件 根据一定的条件 我们就可以从一个数组中快速找出 符合一定条件的元素对象
本次的示范我们沿用上次讲的 NSSortDescriptor 的使用 里面的代码 我们只需要稍微的修改一下 导航栏右边的按钮 为‘搜索年龄大于20的对象’ 然后再把点击左上角按钮的业务代码修改为如下:
- (IBAction)sortAge:(id)sender {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age>20"]; //创建NSPredicate对象 并定义查询条件
self.datas = [[self.datas filteredArrayUsingPredicate:predicate] mutableCopy]; //使用数组的filteredArrayUsingPredicate方法 获取符合我们指定条件的对象
[self.tableView reloadData]; }
这个时候我们点击左上角的按钮 就可以查询出数组里面 年龄大于20的对象
除了 > 号之外 我们还可以用IN 查询两个数组的交集
我们新建一个程序 来测试IN 的用法 我们在ViewDidload方法里面 写入如下代码:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array1 = [NSArray arrayWithObjects:@,@,@,@,@,@,@,@, nil];
NSArray *array2 = [NSArray arrayWithObjects:@,@, nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF in%@",array2]; //SELF 代表本身 IN可以大写也可以小写
NSArray *temp = [array1 filteredArrayUsingPredicate:predicate]; //表示获取 array2 和 array1中的交集
NSLog(@"temp ====\n%@",temp); }
运行 打印结果为:
temp ====
(
4,
6
)
可以看到这两个数组的交集就是4和6 因为这两个数组中 都有4和6 这个元素
同时除了IN 之外 BETWEEN可以获取一定范围的值 示例代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array1 = [NSArray arrayWithObjects:@100,@20,@3,@4,@4,@6,@7,@1, nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN{1,20}"];
NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];
NSLog(@"temp ====\n%@",temp);
}
上面的代码表示找出 值在1到20范围类的值包括首尾
打印结果如下:
temp ====
(
20,
3,
4,
4,
6,
7,
1
)
当然除了上面介绍的两个用法之外 还有其他的比较运算符>,<,==,>=,<=,!=
这里以等于号==为例:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array1 = [NSArray arrayWithObjects:@,@,@,@,@,@,@,@, nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF ==6"];
NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];
NSLog(@"temp ====\n%@",temp);
}
打印结果如下:
temp ====
(
6
)
同时还有以下与字符串操作相关的关键词 :
BEGINSWITH :以某个字符串开头
ENDSWITH :以某个字符串结尾
CONTAINS :是否包含某个字符串
同时这三个关键词后面还可以跟上一些格式符号 如:BEGINSWITH[cd] c表示不区分大小写 d表示不区分发音符号 cd就可以表示即不区分大小写 也不区分发音符号
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil];
//查询出包含e这个字符的字符串
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] 'E' "];
NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];
NSLog(@"temp ====\n%@",temp);
}
打印结果如下:
temp ====
(
anne,
reserved,
type,
soure,
version
)
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil];
//查询出以a这个字符开头的字符串
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] 'a' "];
NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];
NSLog(@"temp ====\n%@",temp);
}
打印结果如下:
temp ====
(
anne
)
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil];
//查询出以e这个字符结尾的字符串
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF ENDSWITH[cd] 'e' "];
NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];
NSLog(@"temp ====\n%@",temp);
}
打印结果如下:
temp ====
(
anne,
type,
soure
)
还有一个你可能会用到的关键字 LIKE 他后面也可以写[cd]格式符号
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil];
//查询出包含e这个字符的字符串
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] '*e*' "]; //*表示通配符
NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];
NSLog(@"temp ====\n%@",temp);
}
打印结果如下:
temp ====
(
anne,
reserved,
type,
soure,
version
)
最后附上所有的相关的条件字符

NSPredicate 的使用的更多相关文章
- 谓词 (NSPredicate)使用详情
谓词 更加详细:http://blog.csdn.net/ztp800201/article/details/8116081 //判断是否满足条件 第一种 判断一个数组(array)中满足条件的 NS ...
- NSPredicate 过滤功能
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"self contains[cd] %@", i ...
- IOS开发之--NSPredicate
我把常用的NSPredicate使用场景整理了一下 官方参考: https://developer.apple.com/library/mac/#documentation/Cocoa/Refer ...
- NSPredicate
NSPredicate 1. 正则表达式使用单个字符串来描述.匹配一系列符合某个句法规则的字符串.通常被用来检索.替换那些符合某个模式的文本. 2. iOS中正则使用 有三种(NSPredicate, ...
- NSPredicate谓词
NSPredicate——谓词(is) 作用:判断条件表达式的求值返回真或假的过程 使用步骤: . 定义NSPredicate对象并指定条件 . 调用谓词的evaluateWithObject方法判断 ...
- 【原/转】iOS中非常强大的过滤器:NSPredicate
在APPLE的官方Demo:UICatalog中实现UISearchBar模糊搜索功能是这么做的: - (void)viewDidLoad { [super viewDidLoad]; self.al ...
- NSPredicate简单应用
1.筛选纯字符串数组的内容 NSArray *array = [[NSArray alloc]initWithObjects:@"beijing",@"shanghai& ...
- iOS开发之--NSPredicate
简述:Cocoa框架中的NSPredicate用于查询,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取. 定义(最常用到的方法): NSPredicate *ca = [NSPred ...
- NSPredicate 根据谓语动词 进行 模糊查询
/** * 模糊查询 */ NSString *filterString = textField.text; NSPredicate *predicate = [NSPredicate predic ...
随机推荐
- java 遍历arrayList的四种方法
package com.test; import java.util.ArrayList;import java.util.Iterator;import java.util.List; public ...
- Zend Studio XDebug调试配置
最近在配置zend studio时找了些资料,发现了这个,说的比较详细 搭建Zend Studio 10.5 和XDebug 环境,试图进行 Drupal的调试, 经历了一些困难,但是最终解决了问题, ...
- 用字符流实现每个文件夹中创建包含所有文件信息的readme.txt
package com.readme; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; i ...
- python读取文本文件
1. 读取文本文件 代码: f = open('test.txt', 'r') print f.read() f.seek(0) print f.read(14) f.seek(0) print f. ...
- $Host.Runspace.ThreadOptions = “ReuseThread”有神马用?
$Host.Runspace.ThreadOptions = “ReuseThread” 在很多PowerShell的脚本中你都会看到这个语句被用来开头,那它的作用是什么呢? 答:这个设置可以提高对内 ...
- Github上传代码菜鸟超详细教程
最近需要将课设代码上传到Github上,之前只是用来fork别人的代码. 这篇文章写得是windows下的使用方法. 第一步:创建Github新账户 第二步:新建仓库 第三部:填写名称,简介(可选 ...
- Redis 集群的合纵与连横
之前一篇写了关于 Redis 的性能,这篇就写写我认为比性能更重要的扩展性方面的主题. 如果再给我一次回到好几年前的机会,对于使用 Redis 我一开始就要好好考虑将来的扩展问题.就像我们做数据库分库 ...
- Javascript判断两个日期是否相等
大家一定遇到过这样的情况,有两个日期对象,然后需要判断他们是否相等. 例如: var date1 = new Date("2013-11-29"); var date2 = new ...
- SEO:让搜索引擎对你的网站更有亲和力(译)
人可以通过查看网站信息了解网站的内容,但是搜索引擎只对标签感兴趣,对内容的识别能力是很低的,如何让蜘蛛通过标签认识你的文章内容呢~ 原文网址:http://schema.org/docs/gs.htm ...
- com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK assets/com.xx.xx
完整的Error 信息(关键部分) Error:Execution failed for task ':fanwe_o2o_47_mgxz_dingzhi:transformResourcesWith ...