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 ...
随机推荐
- Excel VBA 函数
Instr函数 一. 定义 InStr 函数 返回 Variant (Long),指定一字符串在另一字符串中最先出现的位置. InStr([start, ]string1, string2[, com ...
- 学习笔记:Hashtable和HashMap
学了这么些天的基础知识发现自己还是个门外汗,难怪自己一直混的不怎么样.但这样的恶补不知道有没有用,是不是过段时间这些知识又忘了呢?这些知识平时的工作好像都是随拿随用的,也并不是平时一点没有关注过这些基 ...
- [实践] Android5.1.1源码 - 让某个APP以解释执行模式运行
[实践] Android5.1.1源码 - 让某个APP以解释执行模式运行 作者:寻禹@阿里聚安全 前言 本文的实践修改了Android5.1.1的源码. 本文只简单的讲了一下原理.在“实践”一节 ...
- 玩转JavaScript OOP[0]——基础类型
前言 long long ago,大家普遍地认为JavaScript就是做一些网页特效的.处理一些事件的.我身边有一些老顽固的.NET程序员仍然停留在这种认知上,他们觉得没有后端开发肯定是构建不了系统 ...
- 剑指Offer面试题:28.连续子数组的最大和
一.题目:连续子数组的最大和 题目:输入一个整型数组,数组里有正数也有负数.数组中一个或连续的多个整数组成一个子数组.求所有子数组的和的最大值.要求时间复杂度为O(n).例如输入的数组为{1,-2,3 ...
- 新功能发布!Markdown写博客!
有一种神奇的语言,它比html还简单,它巧妙地将内容与格式整合在一起--它就是Markdown. 现在我们实现了博客对Markdown的内置支持,可以让您轻松地在园子里用这个神奇的语言写博客! &qu ...
- 《R in Action》读书笔记(2)
MindMapper 原文件
- 《Entity Framework 6 Recipes》中文翻译系列 (26) ------ 第五章 加载实体和导航属性之延缓加载关联实体和在别的LINQ查询操作中使用Include()方法
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 5-7 在别的LINQ查询操作中使用Include()方法 问题 你有一个LINQ ...
- hibernate学习笔记之四 Hibernate的增删改查
采用JUnit测试,继承TestCase import java.util.Date; import junit.framework.TestCase; import org.hibernate.Se ...
- WaitType:CXPACKET
CXPACKET 等待类型是SQL Server 并发执行一个query时产生的.在run一个big query时,SQL Server充分利用系统的所有资源(CPU,Memory,IO),在最短时间 ...