OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。在Java中是没有这种技术的,但是有开源的框架已经实现了此功能。

下面来看一下具体的例子吧:

Person.h

  1. //
  2. //  Person.h
  3. //  46_NSPredicate
  4. //
  5. //  Created by jiangwei on 14-10-18.
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @interface Person : NSObject
  10. @property NSString *name;
  11. @property NSInteger age;
  12. + (id)personWithName:(NSString *)name andAge:(NSInteger)age;
  13. @end

Person.m

  1. //
  2. //  Person.m
  3. //  46_NSPredicate
  4. //
  5. //  Created by jiangwei on 14-10-18.
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.
  7. //
  8. #import "Person.h"
  9. @implementation Person
  10. + (id)personWithName:(NSString *)name andAge:(NSInteger)age{
  11. Person *person = [[Person alloc] init];
  12. person.name = name;
  13. person.age = age;
  14. return person;
  15. }
  16. - (NSString *)description{
  17. NSString *s =[NSString stringWithFormat:@"name=%@,age=%ld",_name,_age];
  18. return s;
  19. }
  20. @end

我们在Person类中定义属性,还有一个产生对象的类方法,同时重写了description方法,用于打印结果

测试方法

main.m

  1. //
  2. //  main.m
  3. //  46_NSPredicate
  4. //
  5. //  Created by jiangwei on 14-10-18.
  6. //  Copyright (c) 2014年 jiangwei. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "Person.h"
  10. //谓词,指定过滤器的条件,将符合条件的对象保留下来
  11. //一般用谓词过滤数组中指定的元素
  12. int main(int argc, const charchar * argv[]) {
  13. @autoreleasepool {
  14. NSArray *persons = [NSArray arrayWithObjects:
  15. [Person personWithName:@"mac" andAge:20],
  16. [Person personWithName:@"1" andAge:30],
  17. [Person personWithName:@"2" andAge:40],
  18. [Person personWithName:@"3" andAge:50],
  19. [Person personWithName:@"4" andAge:60],
  20. [Person personWithName:@"5" andAge:70],
  21. [Person personWithName:@"6" andAge:20],
  22. [Person personWithName:@"7" andAge:40],
  23. [Person personWithName:@"8" andAge:60],
  24. [Person personWithName:@"9" andAge:40],
  25. [Person personWithName:@"0" andAge:80],
  26. [Person personWithName:@"10" andAge:90],
  27. [Person personWithName:@"1" andAge:20]];
  28. //年龄小于30
  29. //定义谓词对象,谓词对象中包含了过滤条件
  30. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
  31. //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
  32. NSArray *array = [persons filteredArrayUsingPredicate:predicate];
  33. NSLog(@"filterArray=%@",array);
  34. //查询name=1的并且age大于40
  35. predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
  36. array = [persons filteredArrayUsingPredicate:predicate];
  37. NSLog(@"filterArray=%@",array);
  38. //in(包含)
  39. predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];
  40. //name以a开头的
  41. predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
  42. //name以ba结尾的
  43. predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
  44. //name中包含字符a的
  45. predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
  46. //like 匹配任意多个字符
  47. //name中只要有s字符就满足条件
  48. predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
  49. //?代表一个字符,下面的查询条件是:name中第二个字符是s的
  50. predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
  51. }
  52. return 0;
  53. }

首先我们看到,我们初始化了一定大小的数组。

然后我们就可以使用NSPredicate类进行过滤操作了

1、查询数组中年龄小于30的对象

  1. //年龄小于30
  2. //定义谓词对象,谓词对象中包含了过滤条件
  3. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
  4. //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
  5. NSArray *array = [persons filteredArrayUsingPredicate:predicate];
  6. NSLog(@"filterArray=%@",array);

首先创立一个过滤条件:

  1. //定义谓词对象,谓词对象中包含了过滤条件
  2. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];

这里面操作很简单的:@"age<%d",这个age是Person的属性名,%d相当于占位符,然后后面用参数替换即可

然后进行过滤操作,返回一个过滤之后的数组对象

  1. //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
  2. NSArray *array = [persons filteredArrayUsingPredicate:predicate];

2、查询name=1并且age大于40的集合

  1. //查询name=1的并且age大于40
  2. predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
  3. array = [persons filteredArrayUsingPredicate:predicate];
  4. NSLog(@"filterArray=%@",array);

当然我们也可以使用&&进行多条件过滤

3、包含语句的使用

  1. //in(包含)
  2. predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];

4、指定字符开头和指定字符结尾,是否包含指定字符

  1. //name以a开头的
  2. predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
  3. //name以ba结尾的
  4. predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
  5. //name中包含字符a的
  6. predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];

5、like进行匹配多个字符

    1. //like 匹配任意多个字符
    2. //name中只要有s字符就满足条件
    3. predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
    4. //?代表一个字符,下面的查询条件是:name中第二个字符是s的
    5. predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];

谓词(NSPredicate)的更多相关文章

  1. OC中给我们提供的一个技术:谓词(NSPredicate).note

    OC中给我们提供的一个技术:谓词(NSPredicate)OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到 ...

  2. iOS:转载:IOS谓词--NSPredicate

    IOS谓词--NSPredicate 分类: IOS应用2013-02-19 17:24 6792人阅读 评论(1) 收藏 举报 Cocoa 提供了NSPredicate 用于指定过滤条件,谓词是指在 ...

  3. OC学习篇之---谓词(NSPredicate)

    在前一篇文章中我们介绍了OC中一个重要技术通知:http://blog.csdn.net/jiangwei0910410003/article/details/41923401,今天我们在来看一下OC ...

  4. 谓词 (NSPredicate)使用详情

    谓词 更加详细:http://blog.csdn.net/ztp800201/article/details/8116081 //判断是否满足条件 第一种 判断一个数组(array)中满足条件的 NS ...

  5. OC 之 谓词

    NSPredicate 分类: Objective-C iOS XCode Mac2012-10-26 17:26 10557人阅读 评论(1) 收藏 举报 简述:Cocoa框架中的NSPredica ...

  6. coredata中谓词的使用

    Cocoa提供了一个类NSPredicate类,该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配.谓词表示计算真值或假值的函数.在cocoa ...

  7. iOS中谓词的使用

    Cocoa提供了一个类NSPredicate类,该类主要用于指定过滤器的条件,该对象可以准确的描述所需条件,对每个对象通过谓词进行筛选,判断是否与条件相匹配.谓词表示计算真值或假值的函数.在cocoa ...

  8. 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词

    一. 字符串 API 1. NSString 用法简介 (1) NSString API 介绍 NSString 功能 : -- 创建字符串 : 使用 init 开头的实例方法, 也可以使用 Stri ...

  9. NSPredicate的使用

    简述 NSPredicate谓词条件过滤器,一般用于过滤数组数据,原理和用法都类似于SQL中的where,作用相当于数据库的过滤取. 常用函数 创建谓词 + (NSPredicate *)predic ...

随机推荐

  1. RHEL/CentOS/Fedora常用的 CentOS 5/6/7 yum 源(EPEL、Remi、RPMForge、RPMFusion, ius,163,sohu,阿里云)配置

    RHEL以及他的衍生发行版如CentOS.Scientific Linux为了稳定,官方的rpm repository提供的rpm包往往是很滞后的,当然了,这样做这是无可厚非的,毕竟这是服务器版本,安 ...

  2. TFS源代码管理

    一.服务器配置 1.创建一个Visual Studio Online账户 打开VS,选择团队资源管理器(视图菜单下),然后在团队资源管理器中选择注册Team Foundation Service,打开 ...

  3. CodeForces_#354_Div.2_2016.5.25(A+B+C)

    A 描述:给出一串数,可以互换任意两个数的位置一次,求最大的数和最小的数的最大距离. 分析:找到最大的数和最小的数的位置,求右边的数到左端点的距离和左边的数到右端点的距离. #include < ...

  4. sql - and - or

    sql - and SQL AND links together two or more conditional statements for increased filtering when run ...

  5. POJ 2449

    #include<queue> #include<cstdio> #include<string> #include<cstring> #include ...

  6. 尝试加载 Oracle 客户端库时引发 BadImageFormatException。如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题。

    从10G开始,Oracle提供了一个较为轻量级的客户包,叫做Instant Client. 将它安装好后,就不用再安装庞大的Oracle Client了. 这样一来,只要客户端下载Instant Cl ...

  7. We're Hiring A Software Tester

    测试人员的基本修养: 代码编写,不可或缺 乔布斯说:Design is not just what it looks like and feel like,design is how it works ...

  8. boost库的使用(一)

    参考http://www.cnblogs.com/lexus/archive/2012/07/15/2592250.html bjam stage --without-python --toolset ...

  9. mac os 常用终端软件工具

    1. homebrew 安装 网上很多版本返回400错误,以下为最新版本地址(2015/02/09) ruby -e "$(curl -fsSL https://raw.githubuser ...

  10. vijosP1049 送给圣诞夜的礼品

    vijosP1049 送给圣诞夜的礼品 链接:https://vijos.org/p/1049 [思路] 快速幂+矩阵转换. 将m次矩阵的转换看作是一次快速幂中的乘法操作,这样可以用O(log(k/m ...