您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页iOS--谓词

iOS--谓词

来源:二三四教育网

一、基本概念

cocoa中提供了NSPredicate类,指定过滤器的条件,将符合条件的对象保留下来

二、创建谓词的步骤

1、设置谓词条件

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age <= 28"];

2、使用谓词

1) 判断当前对象是满足条件

[predicate evaluateWithObject:id]

2) 将数组中满足条件的内容翻到数组中并返回该数组

[array filteredArrayUsingPredicate:predicate];

三、运算符,谓词中的字符串用单引号括起来

1、逻辑运算符 &&(AND) ||(OR)

[NSPredicate predicateWithFormat:@"age<25 || age>27"];

2、根据关键字查询 IN

[NSPredicate predicateWithFormat:@"name in {'tom-8','jack-3','xxx'}"];

[NSPredicate predicateWithFormat:@"name in %@",inArray];

3、检查某个字是否以**开头  BEGINSWITH

[NSPredicate predicateWithFormat:@"name BEGINSWITH 't' "];

4、检查某个单词是否已**结尾 ENDSWITH

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name ENDSWITH 't' "];

5、是否包含某个字符 CONTAINS

[NSPredicate predicateWithFormat:@"name CONTAINS 'a'"];

6、检查包含某个字符(模糊查询) Like,?和*可作为通配符,其中?匹配1个字符,*匹配0个或者多个字符

[NSPredicate predicateWithFormat:@"name like '??c*'"];

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务