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. Linux改变进程优先级的nice命令

    前言: VPS普遍性能不高,很多人可能有这样一个感受,在执行du.tar等命令时,会造成系统负载飙升,Apache响应缓慢.这时nice命令改变进程优先级可能能缓解这种状况.nice命令用于调整Lin ...

  2. ES6/ES2015核心内容

    ECMAScript定义了: JS语言语法 – 语法解析规则.关键字.语句.声明.运算符等. 类型 – 布尔型.数字.字符串.对象等. 原型和继承 内建对象和函数的标准库 – JSON.Math.数组 ...

  3. CSS clip:rect矩形剪裁功能及一些应用介绍

    CSS clip:rect矩形剪裁功能及一些应用介绍 by zhangxinxu from http://www.zhangxinxu.com本文地址:http://www.zhangxinxu.co ...

  4. ArcGIS Runtime for Android开发教程V2.0(8)基础篇-----地图事件

    转自:http://blog.csdn.net/arcgis_mobile/article/details/8263283 ArcGIS Runtime sdk for Android为我们提供了丰富 ...

  5. vijosP1289 老板娘的促销方案

    vijosP1289 老板娘的促销方案 链接:https://vijos.org/p/1289 [思路] 组合公式+高精度. 如果n-m<2则无解. 否则对于第一个询问:ans=C(n-m,2) ...

  6. Windows Azure 的磁盘管理相关概念

    在 Windows Azure 的虚拟机中,磁盘有多种使用方式.操作系统磁盘是用来为虚拟机提供操作系统的虚拟硬盘.数据磁盘是附加到虚拟机上用来存储应用程序数据的 VHD. 根据应用程序的需要,可从多种 ...

  7. cloudstack安装篇3-SELinux配置、NTP时间同步、配置ClouStack软件库

    一.SELinux配置 为了让CloudStack正常工作,我们必须将SELinux设置为permissive.需要在当前系统运行状态下和启动后都能够生效,进行以下配置. 在系统运行状态下的将SELi ...

  8. Exception in thread "main" java.lang.ClassNotFoundException: 解决方法

    [root@h1 ~]# hadoop jar W1.jar hdfs://h1:9000/hello hdfs://h1:9000/cmd Exception in thread "mai ...

  9. Microsoft SQL Server Data Tools - Business Intelligence for Visual Studio 2013 http://www.microsoft.com/en-us/download/details.aspx?id=42313

    Microsoft SQL Server Data Tools - Business Intelligence for Visual Studio 2013 http://www.microsoft. ...

  10. 问题-[DelphiXE2]提示第三控件不存在

    问题情况:在DelphiXE2启动时界面显示加载了控件,并且控件的路径也放在了环境变量中,但打开程序报第三控件不存在. 问题原因:是没有选择要加载的控件. 问题处理:点击Component->I ...