NSPredicate 的使用总结

来源:互联网 发布:java常量命名规范 编辑:程序博客网 时间:2024/05/29 11:27

NSPredicate 的使用

NSPredicate 是预测的意思 但是我们常翻译成谓词 

它可以干什么?

使用NSPredicate可以定义模糊查询条件 根据一定的条件 我们就可以从一个数组中快速找出 符合一定条件的元素对象

本次的示范我们沿用上次讲的 NSSortDescriptor 的使用 里面的代码 我们只需要稍微的修改一下 导航栏右边的按钮 为‘搜索年龄大于20的对象’ 然后再把点击左上角按钮的业务代码修改为如下:

复制代码
- (IBAction)sortAge:(id)sender {    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age>20"]; //创建NSPredicate对象 并定义查询条件    self.datas =   [[self.datas filteredArrayUsingPredicate:predicate] mutableCopy]; //使用数组的filteredArrayUsingPredicate方法 获取符合我们指定条件的对象
   [self.tableView reloadData]; 

}
复制代码

这个时候我们点击左上角的按钮 就可以查询出数组里面 年龄大于20的对象

除了 > 号之外 我们还可以用IN 查询两个数组的交集

我们新建一个程序 来测试IN 的用法  我们在ViewDidload方法里面 写入如下代码:

复制代码
- (void)viewDidLoad {    [super viewDidLoad];    NSArray *array1 = [NSArray arrayWithObjects:@1,@2,@3,@4,@5,@6,@7,@8, nil];    NSArray *array2 = [NSArray arrayWithObjects:@4,@6, nil];    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF in%@",array2]; //SELF 代表本身 IN可以大写也可以小写
    NSArray *temp = [array1 filteredArrayUsingPredicate:predicate]; //表示获取 array2 和 array1中的交集
NSLog(@"temp ====\n%@",temp); }
复制代码

运行 打印结果为:

temp ====

(

    4,

    6

)

可以看到这两个数组的交集就是4和6 因为这两个数组中 都有4和6 这个元素

同时除了IN 之外 BETWEEN可以获取一定范围的值 示例代码如下:

 

复制代码
- (void)viewDidLoad {    [super viewDidLoad];

      NSArray *array1 = [NSArray arrayWithObjects:@100,@20,@3,@4,@4,@6,@7,@1, nil];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN{1,20}"];    NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];    NSLog(@"temp ====\n%@",temp);}
复制代码

 

上面的代码表示找出 值在1到20范围类的值包括首尾

打印结果如下:

temp ====

(

    20,

    3,

    4,

    4,

    6,

    7,

    1

)

当然除了上面介绍的两个用法之外 还有其他的比较运算符>,<,==,>=,<=,!=

这里以等于号==为例:

复制代码
- (void)viewDidLoad {    [super viewDidLoad];    NSArray *array1 = [NSArray arrayWithObjects:@100,@20,@3,@4,@4,@6,@7,@1, nil];    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF ==6"];    NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];    NSLog(@"temp ====\n%@",temp);}
复制代码

打印结果如下:

temp ====

(

    6

)

同时还有以下与字符串操作相关的关键词 :

BEGINSWITH  :以某个字符串开头

ENDSWITH     :以某个字符串结尾

CONTAINS      :是否包含某个字符串

同时这三个关键词后面还可以跟上一些格式符号 如:BEGINSWITH[cd] c表示不区分大小写 d表示不区分发音符号 cd就可以表示即不区分大小写 也不区分发音符号

复制代码
- (void)viewDidLoad {    [super viewDidLoad];    NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil];    //查询出包含e这个字符的字符串    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] 'E' "];    NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];    NSLog(@"temp ====\n%@",temp);}
复制代码

打印结果如下:

temp ====

(

    anne,

    reserved,

    type,

    soure,

    version

)

 

复制代码
- (void)viewDidLoad {    [super viewDidLoad];    NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil];    //查询出以a这个字符开头的字符串    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH[cd] 'a' "];    NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];    NSLog(@"temp ====\n%@",temp);}
复制代码

打印结果如下:

temp ====

(

    anne

)

 

复制代码
- (void)viewDidLoad {    [super viewDidLoad];    NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil];    //查询出以e这个字符结尾的字符串    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF ENDSWITH[cd] 'e' "];    NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];    NSLog(@"temp ====\n%@",temp);}
复制代码

 

打印结果如下:

temp ====

(

    anne,

    type,

    soure

)

 

 

还有一个你可能会用到的关键字 LIKE  他后面也可以写[cd]格式符号

复制代码
- (void)viewDidLoad {    [super viewDidLoad];    NSArray *array1 = [NSArray arrayWithObjects:@"jack",@"anne",@"reserved",@"control" ,@"type",@"soure",@"version",nil];    //查询出包含e这个字符的字符串    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF like[cd] '*e*' "]; //*表示通配符    NSArray *temp = [array1 filteredArrayUsingPredicate:predicate];    NSLog(@"temp ====\n%@",temp);}
复制代码

打印结果如下:

temp ====

(

    anne,

    reserved,

    type,

    soure,

    version

)

 

最后附上所有的相关的条件字符

 

 

 

原创粉丝点击