NSPredicate 的使用

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

)

 

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

 

 

 

转载于:https://www.cnblogs.com/syios/p/5924630.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值