OC中给我们提供的一个技术:谓词(NSPredicate).note
OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。在Java中是没有这种技术的,但是有开源的框架已经实现了此功能。
下面来看一下具体的例子吧:
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.
9. #import <Foundation/Foundation.h>
10.
11. @interface Person : NSObject
12.
13. @property NSString *name;
14. @property NSInteger age;
15.
16. + (id)personWithName:(NSString *)name andAge:(NSInteger)age;
17.
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.
9. #import "Person.h"
10.
11. @implementation Person
12.
13. + (id)personWithName:(NSString *)name andAge:(NSInteger)age{
14. Person *person = [[Person alloc] init];
15. person.name = name;
16. person.age = age;
17. return person;
18. }
19.
20. - (NSString *)description{
21. NSString *s =[NSString stringWithFormat:@"name=%@,age=%ld",_name,_age];
22. return s;
23. }
24.
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.
9. #import <Foundation/Foundation.h>
10. #import "Person.h"
11.
12. //谓词,指定过滤器的条件,将符合条件的对象保留下来
13. //一般用谓词过滤数组中指定的元素
14. int main(int argc, const charchar * argv[]) {
15. @autoreleasepool {
16.
17. NSArray *persons = [NSArray arrayWithObjects:
18. [Person personWithName:@"mac" andAge:20],
19. [Person personWithName:@"1" andAge:30],
20. [Person personWithName:@"2" andAge:40],
21. [Person personWithName:@"3" andAge:50],
22. [Person personWithName:@"4" andAge:60],
23. [Person personWithName:@"5" andAge:70],
24. [Person personWithName:@"6" andAge:20],
25. [Person personWithName:@"7" andAge:40],
26. [Person personWithName:@"8" andAge:60],
27. [Person personWithName:@"9" andAge:40],
28. [Person personWithName:@"0" andAge:80],
29. [Person personWithName:@"10" andAge:90],
30. [Person personWithName:@"1" andAge:20]];
31.
32. //年龄小于30
33. //定义谓词对象,谓词对象中包含了过滤条件
34. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
35. //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
36. NSArray *array = [persons filteredArrayUsingPredicate:predicate];
37. NSLog(@"filterArray=%@",array);
38.
39. //查询name=1的并且age大于40
40. predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
41. array = [persons filteredArrayUsingPredicate:predicate];
42. NSLog(@"filterArray=%@",array);
43.
44. //in(包含)
45. predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];
46.
47. //name以a开头的
48. predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
49. //name以ba结尾的
50. predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
51.
52. //name中包含字符a的
53. predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
54.
55. //like 匹配任意多个字符
56. //name中只要有s字符就满足条件
57. predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
58. //?代表一个字符,下面的查询条件是:name中第二个字符是s的
59. predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
60.
61.
62.
63. }
64. return 0;
1. //年龄小于30
2. //定义谓词对象,谓词对象中包含了过滤条件
3. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
4. //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
5. NSArray *array = [persons filteredArrayUsingPredicate:predicate];
1. //定义谓词对象,谓词对象中包含了过滤条件
1. //使用谓词条件过滤数组中的元素,过滤之后返回查询的结果
1. //查询name=1的并且age大于40
2. predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
3. array = [persons filteredArrayUsingPredicate:predicate];
1. //in(包含)
1. //name以a开头的
2. predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
3. //name以ba结尾的
4. predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
5.
6. //name中包含字符a的
1. //like 匹配任意多个字符
2. //name中只要有s字符就满足条件
3. predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
4. //?代表一个字符,下面的查询条件是:name中第二个字符是s的
转载于:https://www.cnblogs.com/GhostKZShadow/p/5105470.html
OC中给我们提供的一个技术:谓词(NSPredicate).note相关推荐
- 字符串在JVM中如何存放 及常量池技术
字符串对象在JVM中可能有两个存放的位置:字符串常量池或堆内存. 使用常量字符串初始化的字符串对象,它的值存放在字符串常量池中: 使用字符串构造方法创建的字符串对象,它的值存放在堆内存中: Strin ...
- OC 中的关键词 大全(1)
1. @interface和@implementation 分别在.h和.m文件开头处 @interface和@implementation都可以说是接口 oc中的类必须包括两部分,interface ...
- OC中@interface、@implementation、@protocal
Java中的interface和implementation OC中的interface和implementation protocol 注意 Java中的interface和implementati ...
- 在各种xDSL技术中,能提供上下行信道非对称传输的是______。正确答案 B
在各种xDSL技术中,能提供上下行信道非对称传输的是______. A.ADSL和HDSL B.ADSL和VDSL C.SDSL和VDSL D.SDSL和HDSL 正确答案 B 答案解析 [解析]: ...
- oc中怎么写c语言函数,献给Objective-C的初学者--入门篇(一)初识OC
写这篇文章的目的: 笔者其实也是初学者,所以笔者理解想要入门又不知道怎么办的同道中人那种心情:同时也是笔者对所学的做一个回顾.好了废话不多说,下面开始正题. OC是一门面向对象的语言 那么什么是面向对 ...
- iOS底层探索二(OC 中 alloc 方法 初探)
前言 相关文章: iOS底层探索一(底层探索方法) iOS底层探索三(内存对齐与calloc分析) iOS底层探索四(isa初探-联合体,位域,内存优化) iOS底层探索五(isa与类的关系) iOS ...
- OC中常见面试题汇整篇
当页面跳转的时候,如何实现导航条和页面同时出现?因为他两个经常出现不同步的现象? 如何将tablview强制转换成UIScrollow? 当一个界面在6s上正好可以完全呈现,那在4s上面你是如何处理的 ...
- iOS开发几年了,你清楚OC中的这些东西么!!!?
iOS开发几年了,你清楚OC中的这些东西么!!!? 前言 几年前笔者是使用Objective-C进行iOS开发, 不过在两年前Apple发布swift的时候,就开始了swift的学习, 在swift1 ...
- 命保住了!五年时间,我们也搞了一个技术中台
题图: from Zoommy 来自:吃草的罗汉公众号 在技术圈,每年总有那么一拨人,喜欢把 "某某岗位要不要写代码" 这样的话题拿出来讨论一番. 比如,前年的话题是CTO要不要写 ...
最新文章
- 阿里官方Redis开发规范!
- html5复选框样式,11种炫酷CSS3复选框checkbox样式美化效果
- tsconfig.json编译选项
- hdu4848 DFS 暴搜+ 强剪枝
- Redhat中通过命令工具和配置文件设置TCP/IP参数的方法
- 死磕java并发cas_死磕 java并发包之AtomicInteger源码分析
- 计算机图形学在GIS中的应用,GIS在交通中的应用与发展
- (九)JS-WEB-API(DOM、BOM、事件、Ajax、存储)【每个工程师必须熟练掌握的技能】
- 多线程设计模式:第二篇 - 四种基础模式
- microsoft azure
- html dom阮一峰,DOM - 概述 - 《阮一峰 JavaScript 教程》 - 书栈网 · BookStack
- 微博开发笔记上(未完待续)
- 微信小程序视频学习教程
- 兄弟HL4150cdn恢复出厂设置_兄弟打印机恢复出厂设置
- mybatis事务原理与集成spring的事务原理
- 图片服务器的架构演进
- 文本预处理:词袋模型(bag of words,BOW)、TF-IDF
- C语言零基础入门级数据类型+运算符+控制流+ASCII 码表+面试题【系统学习第二天】
- kafka之broker
- objective-c 语法快速过(5)