在前一篇文章中我们介绍了OC中一个重要技术通知: http://blog.csdn.net/leo_dli/article/details/50366436,今天我们在来看一下OC中给我们提供的一个技术:谓词(NSPredicate)
OC中的谓词操作是针对于数组类型的,他就好比数据库中的查询操作,数据源就是数组,这样的好处是我们不需要编写很多代码就可以去操作数组,同时也起到过滤的作用,我们可以编写简单的谓词语句,就可以从数组中过滤出我们想要的数据。非常方便。在Java中是没有这种技术的,但是有开源的框架已经实现了此功能。
下面来看一下具体的例子吧:
Person.h
-
-
-
-
-
-
-
-
- #import <Foundation/Foundation.h>
-
- @interface Person : NSObject
-
- @property NSString *name;
- @property NSInteger age;
-
- + (id)personWithName:(NSString *)name andAge:(NSInteger)age;
-
- @end
Person.m
-
-
-
-
-
-
-
-
- #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
-
-
-
-
-
-
-
-
- #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]];
-
-
-
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age<%d",30];
-
- NSArray *array = [persons filteredArrayUsingPredicate:predicate];
- NSLog(@"filterArray=%@",array);
-
-
- predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
- array = [persons filteredArrayUsingPredicate:predicate];
- NSLog(@"filterArray=%@",array);
-
-
- predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];
-
-
- predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
-
- predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
-
-
- predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
-
-
-
- predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
-
- predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
-
-
-
- }
- return 0;
- }
首先我们看到,我们初始化了一定大小的数组。
然后我们就可以使用NSPredicate类进行过滤操作了
1、查询数组中年龄小于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的集合
-
- predicate = [NSPredicate predicateWithFormat:@"name='1' && age>40"];
- array = [persons filteredArrayUsingPredicate:predicate];
- NSLog(@"filterArray=%@",array);
当然我们也可以使用&&进行多条件过滤
3、包含语句的使用
-
- predicate = [NSPredicate predicateWithFormat:@"self.name IN {'1','2','4'} || self.age IN{30,40}"];
4、指定字符开头和指定字符结尾,是否包含指定字符
-
- predicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'a'"];
-
- predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 'ba'"];
-
-
- predicate = [NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];
5、like进行匹配多个字符
-
-
- predicate = [NSPredicate predicateWithFormat:@"name like '*s*'"];
-
- predicate = [NSPredicate predicateWithFormat:@"name like '?s'"];
总结
这一篇就介绍了OC中常用的技术:谓词的使用,他用起来很方便的,而且也没什么难度,和我们当初在操作数据库的时候很想,但是他对我们进行过滤操作提供了很大的便捷。