NSPredicate的使用
简述
NSPredicate谓词条件过滤器,一般用于过滤数组数据,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取。
常用函数
- 创建谓词
+ (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat, ...;
- 使用谓词过滤集合
NSarray过滤
//使用指定的谓词过滤NSArray集合,返回符合条件的元素组成的新集合
- (NSArray*)filteredArrayUsingPredicate:(NSPredicate *)predicate;
NSMutableArray过滤
//使用指定的谓词过滤NSMutableArray,剔除集合中不符合条件的元素
- (void)filterUsingPredicate:(NSPredicate *)predicate;
NSSet过滤
- (NSSet*)filteredSetUsingPredicate:(NSPredicate *)predicate;
NSMutableSet过滤
- (void)filterUsingPredicate:(NSPredicate *)predicate;
NSOrderedSet过滤
- (NSOrderedSet<ObjectType> *)filteredOrderedSetUsingPredicate:(NSPredicate *)p;
NSMutableOrderedSet过滤
- (void)filterUsingPredicate:(NSPredicate *)p;
使用
创建模型类Person,包含name和age两个属性
初始化数据源数组
Person *person1 = [Person initWithName:@"alex" andAge:22];
Person *person2 = [Person initWithName:@"ceciliaba" andAge:35];
Person *person3 = [Person initWithName:@"1" andAge:42];
Person *person4 = [Person initWithName:@"otto" andAge:18];
Person *person5 = [Person initWithName:@"yasha2" andAge:16];
NSArray *personArr = [NSArray arrayWithObjects:person1,person2,person3,person4,person5, nil];
定义谓词对象,设置过滤条件(过滤条件中,使用self.name和直接用name的效果一样)
//age小于30
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<30"];
//查询name=1的并且age大于40
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
//name以a开头的
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
//name以ba结尾的
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
//name为1/2/4,或者age在30-40之间的
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name IN {'1','2','4'} || age between{30,40}"];
//like 匹配任意多个字符
//name中只要有s字符就满足条件
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
//?代表一个字符,下面的查询条件是:name中第二个字符是s的 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
NSArray *array = [personArr filteredArrayUsingPredicate:predicate];
使用占位符,动态修改条件
在使用时,如果需要拼接属性名,其占位符为%K(注意大写)而不是%@,如:
NSString * key = @"age";
int age = 30;
//拼接示例:
[NSPredicate predicateWithFormat:@"%K < %d", key, age];
如果想动态改变判断的范围,可以使用$ 开头的占位符:
//用$AGE进行占位,可以动态修改$对应的值,这里的AGE可以是任意字符串
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age < $AGE"];
//修改AGE的值(AGE对应上面的$后的字符串),生成新的NSPredicate对象
NSPredicate *newPredicate = [predicate predicateWithSubstitutionVariables:@{@"AGE":@30}];
//使用newPredicate过滤数组
NSArray *array = [persons filteredArrayUsingPredicate: newPredicate];
使用附加符号增加规则
附加符号:[c] [d] [cd] c表示不区分大小写,d表示不区分发音字符,cd表示什么都不区分
错误用法
NSArray *array = [NSArray arrayWithObjects:@{@"city":@"beijing"},@{@"city":@"shanghai"},@{@"city":@"guangzhou"},@{@"city":@"wuhan"}, nil];
NSString *string = @"ang";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",string];
NSMutableArray *tempArr = [NSMutableArray new];
for (NSDictionary *dic in array) {
if ([pred evaluateWithObject:dic[@"city"]]) {
[tempArr addObject:dic];
}
}
NSLog(@"tempArr = %@",tempArr);
输出:
tempArr = (
{
city = shanghai;
},
{
city = guangzhou;
}
)
这种用法虽然也能过滤出想要的数据,但是效率不高,后面的for循环其实可以省略的
正确用法
NSPredicate *pred = [NSPredicate predicateWithFormat:@"city CONTAINS[cd] %@",string];
NSArray *resultArr = [array filteredArrayUsingPredicate:pred];
NSLog(@"resultArr = %@",resultArr);
输出:
resultArr = (
{
city = shanghai;
},
{
city = guangzhou;
}
)
NSPredicate的使用的更多相关文章
- NSPredicate 的使用
NSPredicate是什么? NSPredicate 是预测的意思 但是我们常翻译成谓词 它可以干什么? 使用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 ...
随机推荐
- async 配合mysql
async-db.js const mysql = require('mysql') const pool = mysql.createPool({ host : '127.0.0.1', user ...
- Ajax 的几种方法应用
一,js实现ajax异步请求,简单例子 try.jsp <%@ page language="java" import="java.util.*" pag ...
- 【代码笔记】iOS-左右可滑动的选择条
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- Andoid多语言国际化策略
目前手上的项目,为了普及覆盖更多的用户群,也已经开始实现了多语言设置这样的功能,不过今天我要说的不是微信,而是我们自己项目中的实现策略. 直接附上关键代码: package com.huolonglu ...
- Android ListView的XML属性
1.ListView的XML属性 android:divider //在列表条目之间显示的drawable或color android:dividerHeight //用来指定divider的高度 a ...
- cookie implements session
cookie实现会话 服务器调用response.addCookie()设置set-cookie响应头后,浏览器收到这个响应头与数值后,会将它以文件的形式存储于本地PC上.当浏览器再次访问同一Web服 ...
- Week5——Ajax
1.简介 AJAX 相当于异步 JavaScript 和 XML,是一种用于创建快速动态网页的技术.通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网 ...
- Eigen学习之简单线性方程与矩阵分解
Eigen提供了解线性方程的计算方法,包括LU分解法,QR分解法,SVD(奇异值分解).特征值分解等.对于一般形式如下的线性系统: 解决上述方程的方式一般是将矩阵A进行分解,当然最基本的方法是高斯消元 ...
- [Asp.net mvc]Asp.net mvc 使用Json传递数据
在之前的练习中一直是直接传递的Model到后台或是单个数据到后台,今天在使用中遇到了点问题,不能使用Model传递到后台,但又要实现多个数据到后台,实验了多次有了以下的解决方案,给自己留个笔记. 功能 ...
- log4j配置详解(非常详细)
Log4J的配置文件(Configuration File)就是用来设置记录器的级别.存放器和布局的,它可接key=value格式的设置或xml格式的设置信息.通过配置,可以创建出Log4J的运行环境 ...