在前一篇文章中我们介绍了OC中一个重要技术通知:http://blog.csdn.net/jiangwei0910410003/article/details/41923401,今天我们在来看一下OC中给我们提供的一个技术:谓词(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 char * 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'"];

总结

这一篇就介绍了OC中常用的技术:谓词的使用,他用起来很方便的,而且也没什么难度,和我们当初在操作数据库的时候很想,但是他对我们进行过滤操作提供了很大的便捷。

OC学习篇之---谓词(NSPredicate)的更多相关文章

  1. OC学习篇之---总结和学习目录

    今天终于把OC的基础知识学习完了,但是这些知识只是最基础的,还有很多高级知识,这个可能需要后面慢慢的去学习才能体会到.下面就是这次学习OC的目录教程,如果大家发现有什么不正确的地方,请指正,小弟是新生 ...

  2. OC学习篇之---单例模式

    在之前的一片文章中介绍了对象的拷贝相关知识:http://blog.csdn.net/jiangwei0910410003/article/details/41926531,今天我们来看一下OC中的单 ...

  3. (转载)OC学习篇之---概述

    前言 终于开启了OC的学习篇了,之前由于工作上的事,学习就一直搁浅了,不过最近由于各种原因,感觉必须要开启iOS的开发旅程了,不然就老了.因为之前一直是做Android的,所以学习iOS来就没那么费劲 ...

  4. (转载)OC学习篇之---类的三大特性:封装,继承,多态

    之前的一片文章介绍了OC中类的初始化方法和点语法的使用,今天来继续学习OC中的类的三大特性,我们在学习Java的时候都知道,类有三大特性:继承,封装,多态,这个也是介绍类的时候,必须提到的话题,那么今 ...

  5. (转载)OC学习篇之---第一个程序HelloWorld

    之前的一片文章简单的介绍了OC的相关概述,从这篇开始我们就开始学习OC的相关知识了,在学习之前,个人感觉需要了解的其他的两门语言:一个是C/C++,一个是面向对象的语言(当然C++就是面向对象,不过这 ...

  6. OC学习篇之---第一个程序HelloWorld

    从这篇开始我们就开始学习OC的相关知识了,在学习之前,个人感觉需要了解的其他的两门语言:一个是C/C++,一个是面向对象的语言(当然C++就是面向对象,不过这里最好还是Java).在干活之前,得先找到 ...

  7. OC学习篇之---内存管理介绍和使用

    在之前的一片文章我们说了OC中谓词操作:http://blog.csdn.net/jiangwei0910410003/article/details/41923507,从今天开始我们就来看一下OC中 ...

  8. OC学习篇之---类的三大特性(封装,继承,多态)

    之前的一片文章介绍了OC中类的初始化方法和点语法的使用:http://blog.csdn.net/jiangwei0910410003/article/details/41683873,今天来继续学习 ...

  9. (转载)OC学习篇之---归档和解挡

    前几篇文章说到了OC中的Foundation框架,今天我们来看一下OC中的一个重要知识点:归档 OC中的归档就是将对象写入到一个文件中,Java中的ObjectInputStream和ObjectOu ...

随机推荐

  1. C# 私有字段前缀 _ 的设置(VS2019, .editorconfig)

    常量和静态只读字段大写 私有字段前缀 _ #### Naming styles #### # Naming rules dotnet_naming_rule.const_should_be_all_u ...

  2. BottomNavigationBar 自定义 底部导航条

    在flutter中,BottomNavigationBar 是底部导航条,可以让我们定义底部 Tab 切换,bottomNavigationBar是 Scaffold 组件的参数. BottomNav ...

  3. python3安装pdfminer并使用

    1.python3不同与2版本不能使用pdfminer pip install pdfminer3k 2.使用pdfminer解析相应文档并保存到相应的文件夹中 # encoding : udf-8 ...

  4. Visible Lattice Points

    题目链接 题意:给个N*N的矩形点,求在原点看去能看到多少个点 思路:除了(1,0),(0,1),(1,1)外其他点的xy都互质.所以求欧拉函数.fhi[i]从2加到n,再是两倍,再加3. #incl ...

  5. ElasticSearch 简介概念及核心

    1.ES是什么 ES是面向文档的Nosql,这意味着它可以存储整个对象或文档.然而它不仅仅是存储,还会索引(index)每个文档的内容使之可以被搜索.在es中,你可以对文档(而非成行成列的数据)进行索 ...

  6. liunx 上无法kill 掉 redis服务

    要新学习一下redis 的哨兵服务,但是发现启动redis的时候,哨兵服务已经存在了,而且reids6379的服务也杀不死,就找到这样的参考方案 /etc/init.d/redis-server st ...

  7. 八. jenkins参数化构建 git分支

    使用git管理代码时往往会有多分支开发,这时部署通过修改配置不试用.可以通过参数化构建. 1.通过字符参数方式 新建一个maven项目,选择参数化构建过程-字符参数,如下: 源码管理中配置如下: 其它 ...

  8. USB入门开发的八个问题&USB枚举『转』

    USB 基本知识 USB的重要关键字: 1.端点:位于USB设备或主机上的一个数据缓冲区,用来存放和发送USB的各种数据,每一个端点都有惟一的确定地址,有不同的传输特性(如输入端点.输出端点.配置端点 ...

  9. CSS-给Font Awesome拓展Base64编码的图标

    和 fa 一样设置到::before中就行了,不过 fa 是直接设置内容,这里用的背景图 .fa-science-garden::before { content: ""; dis ...

  10. 【玩转SpringBoot】异步任务执行与其线程池配置

    同步代码写起来简单,但就是怕遇到耗时操作,会影响效率和吞吐量. 此时异步代码才是王者,但涉及多线程和线程池,以及异步结果的获取,写起来颇为麻烦. 不过在遇到SpringBoot异步任务时,这个问题就不 ...