谓词(NSPredicate)
OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。在Java中是没有这种技术的,但是有开源的框架已经实现了此功能。
下面来看一下具体的例子吧:
Person.h
- //
- // Person.h
- // 46_NSPredicate
- //
- // Created by jiangwei on 14-10-18.
- // Copyright (c) 2014年 jiangwei. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface Person : NSObject
- @property NSString *name;
- @property NSInteger age;
- + (id)personWithName:(NSString *)name andAge:(NSInteger)age;
- @end
Person.m
- //
- // Person.m
- // 46_NSPredicate
- //
- // Created by jiangwei on 14-10-18.
- // Copyright (c) 2014年 jiangwei. All rights reserved.
- //
- #import "Person.h"
- @implementation Person
- + (id)personWithName:(NSString *)name andAge:(NSInteger)age{
- Person *person = [[Person alloc] init];
- person.name = name;
- person.age = age;
- return person;
- }
- - (NSString *)description{
- NSString *s =[NSString stringWithFormat:@"name=%@,age=%ld",_name,_age];
- return s;
- }
- @end
我们在Person类中定义属性,还有一个产生对象的类方法,同时重写了description方法,用于打印结果
测试方法
main.m
- //
- // main.m
- // 46_NSPredicate
- //
- // Created by jiangwei on 14-10-18.
- // Copyright (c) 2014年 jiangwei. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Person.h"
- //谓词,指定过滤器的条件,将符合条件的对象保留下来
- //一般用谓词过滤数组中指定的元素
- int main(int argc, const charchar * argv[]) {
- @autoreleasepool {
- NSArray *persons = [NSArray arrayWithObjects:
- [Person personWithName:@"mac" andAge:20],
- [Person personWithName:@"1" andAge:30],
- [Person personWithName:@"2" andAge:40],
- [Person personWithName:@"3" andAge:50],
- [Person personWithName:@"4" andAge:60],
- [Person personWithName:@"5" andAge:70],
- [Person personWithName:@"6" andAge:20],
- [Person personWithName:@"7" andAge:40],
- [Person personWithName:@"8" andAge:60],
- [Person personWithName:@"9" andAge:40],
- [Person personWithName:@"0" andAge:80],
- [Person personWithName:@"10" andAge:90],
- [Person personWithName:@"1" andAge:20]];
- //年龄小于30
- //定义谓词对象,谓词对象中包含了过滤条件
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
- //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
- NSArray *array = [persons filteredArrayUsingPredicate:predicate];
- NSLog(@"filterArray=%@",array);
- //查询name=1的并且age大于40
- predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
- array = [persons filteredArrayUsingPredicate:predicate];
- NSLog(@"filterArray=%@",array);
- //in(包含)
- predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];
- //name以a开头的
- predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
- //name以ba结尾的
- predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
- //name中包含字符a的
- predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
- //like 匹配任意多个字符
- //name中只要有s字符就满足条件
- predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
- //?代表一个字符,下面的查询条件是:name中第二个字符是s的
- predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
- }
- return 0;
- }
首先我们看到,我们初始化了一定大小的数组。
然后我们就可以使用NSPredicate类进行过滤操作了
1、查询数组中年龄小于30的对象
- //年龄小于30
- //定义谓词对象,谓词对象中包含了过滤条件
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
- //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
- NSArray *array = [persons filteredArrayUsingPredicate:predicate];
- NSLog(@"filterArray=%@",array);
首先创立一个过滤条件:
- //定义谓词对象,谓词对象中包含了过滤条件
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
这里面操作很简单的:@"age<%d",这个age是Person的属性名,%d相当于占位符,然后后面用参数替换即可
然后进行过滤操作,返回一个过滤之后的数组对象
- //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
- NSArray *array = [persons filteredArrayUsingPredicate:predicate];
2、查询name=1并且age大于40的集合
- //查询name=1的并且age大于40
- predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
- array = [persons filteredArrayUsingPredicate:predicate];
- NSLog(@"filterArray=%@",array);
当然我们也可以使用&&进行多条件过滤
3、包含语句的使用
- //in(包含)
- predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];
4、指定字符开头和指定字符结尾,是否包含指定字符
- //name以a开头的
- predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
- //name以ba结尾的
- predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
- //name中包含字符a的
- predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
5、like进行匹配多个字符
- //like 匹配任意多个字符
- //name中只要有s字符就满足条件
- predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
- //?代表一个字符,下面的查询条件是:name中第二个字符是s的
- predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
谓词(NSPredicate)的更多相关文章
- OC中给我们提供的一个技术:谓词(NSPredicate).note
OC中给我们提供的一个技术:谓词(NSPredicate)OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到 ...
- iOS:转载:IOS谓词--NSPredicate
IOS谓词--NSPredicate 分类: IOS应用2013-02-19 17:24 6792人阅读 评论(1) 收藏 举报 Cocoa 提供了NSPredicate 用于指定过滤条件,谓词是指在 ...
- OC学习篇之---谓词(NSPredicate)
在前一篇文章中我们介绍了OC中一个重要技术通知:http://blog.csdn.net/jiangwei0910410003/article/details/41923401,今天我们在来看一下OC ...
- 谓词 (NSPredicate)使用详情
谓词 更加详细:http://blog.csdn.net/ztp800201/article/details/8116081 //判断是否满足条件 第一种 判断一个数组(array)中满足条件的 NS ...
- OC 之 谓词
NSPredicate 分类: Objective-C iOS XCode Mac2012-10-26 17:26 10557人阅读 评论(1) 收藏 举报 简述:Cocoa框架中的NSPredica ...
- coredata中谓词的使用
Cocoa提供了一个类NSPredicate类,该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配.谓词表示计算真值或假值的函数.在cocoa ...
- iOS中谓词的使用
Cocoa提供了一个类NSPredicate类,该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配.谓词表示计算真值或假值的函数.在cocoa ...
- 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词
一. 字符串 API 1. NSString 用法简介 (1) NSString API 介绍 NSString 功能 : -- 创建字符串 : 使用 init 开头的实例方法, 也可以使用 Stri ...
- NSPredicate的使用
简述 NSPredicate谓词条件过滤器,一般用于过滤数组数据,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取. 常用函数 创建谓词 + (NSPredicate *)predic ...
随机推荐
- [LeetCode#272] Closest Binary Search Tree Value II
Problem: Given a non-empty binary search tree and a target value, find k values in the BST that are ...
- oracle中查看某个用户名下所有的表以及sequence
select table_name from all_tables where owner =upper('jdfp') ; 此处查的是tieba这个用户表空间下的所有表 ...
- 【转】CUDA5/CentOS6.4
转载自http://wenzhang.baidu.com/article/view?key=076f36658dd0828c-1393896446 This article explains how ...
- c++ 11 key note
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- Storm系列(十九)普通事务ITransactionalSpout及示例
普通事务API详解 1 _curtxid:" + _curtxid 46 + ",_tx.getTransactionId():&qu ...
- 【Java基础】Java中的多态
什么是多态 多态是一个对象的多种实现,是建立在继承的基础上的,即对象“人”,有老师和学生不同的实现,其实总结起来就是允许将子类类型的指针赋值给父类类型的指针. 多态的发生条件 多态发生的前提是:1. ...
- HTML 网页游戏 2048
新手只会一点html和css,javascript基本不会,更别提jQuery了= = 跟着慕课网的教学视频(视频地址:http://www.imooc.com/learn/76)一点点做的,由于自己 ...
- Storm与Hadoop的角色和组件比较
Storm与Hadoop的角色和组件比较 Storm 集群和 Hadoop 集群表面上看很类似.但是 Hadoop 上运行的是 MapReduce 作业,而在 Storm 上运行的是拓扑 Topolo ...
- 跟我一起写Makefile:MakeFile介绍
makefile 介绍 make命令执行时,需要一个 makefile 文件,以告诉make命令如何去编译和链接程序. 首先,我们用一个示例来说明makefile的书写规则.以便给大家一个感性认识.这 ...
- 教程-Delphi操作快捷键
************************************************************** Delphi快捷键-全-高手用-南山古桃(新手)-同学共进 ******* ...