iOS中NSPredicate的使用方法
来源:互联网 发布:股指期货实时数据接口 编辑:程序博客网 时间:2024/05/18 03:59
Cocoa supports a wide range of types of predicate, including the following:
Simple comparisons, such as
grade == 7
orfirstName like 'Mark'
Case or diacritic insensitive lookups, such as
name contains[cd] 'citroen'
Logical operations, such as
(firstName beginswith 'M') AND (lastName like 'Adderley')
You can use predicates with any class of object, but a class must be key-value coding compliant for the keys you want to use in a predicate.
操作符:
1.比较:>,<,==,>=,<=,!=
2.范围:IN,BETWEEN
3.字符串本身:SELF
4.字符串相关:BEGINSWITH、ENDSWITH、CONTAINS
5.通配符:LIKE
6.正则表达式:MATCHES
使用步骤:
a.创建NSPredicate对象
b.调用NSPredicate对象的evaluateWithObject:方法,传入的参数即为需要判断的对象
应用:
1.字符串自身
NSString *value = @"zhang"; NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF == 'zhang'"]; BOOL result = [predicate evaluateWithObject:value];//result = YES predicate = [NSPredicate predicateWithFormat:@"SELF contains 'an'"]; result = [predicate evaluateWithObject:value];//result = YES predicate = [NSPredicate predicateWithFormat:@"SELF contains 'aN'"]; result = [predicate evaluateWithObject:value];//result = NO,[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。 predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] 'aN'"]; result = [predicate evaluateWithObject:value];//result = YES predicate = [NSPredicate predicateWithFormat:@"SELF contains 'hh'"]; result = [predicate evaluateWithObject:value];//result = NO predicate = [NSPredicate predicateWithFormat:@"SELF beginswith 'zha'"]; result = [predicate evaluateWithObject:value];//result = YES predicate = [NSPredicate predicateWithFormat:@"SELF beginswith 'ha'"]; result = [predicate evaluateWithObject:value];//result = NO predicate = [NSPredicate predicateWithFormat:@"SELF endswith 'ang'"]; result = [predicate evaluateWithObject:value];//result = YES predicate = [NSPredicate predicateWithFormat:@"SELF endswith 'an'"]; result = [predicate evaluateWithObject:value];//result = NO predicate = [NSPredicate predicateWithFormat:@"SELF like '*an*'"]; result = [predicate evaluateWithObject:value];//result = YES
2.key-value 对象
NSString *key = @"key"; NSString *value = @"zhang"; NSDictionary *dic = @{key:value}; NSPredicate * predicate = [NSPredicate predicateWithFormat:@"key == 'zhang'"]; BOOL result = [predicate evaluateWithObject:dic];//result = YES predicate = [NSPredicate predicateWithFormat:@"key contains 'an'"]; result = [predicate evaluateWithObject:dic];//result = YES predicate = [NSPredicate predicateWithFormat:@"key contains 'aN'"]; result = [predicate evaluateWithObject:dic];//result = NO,[c]不区分大小写[d]不区分发音符号即没有重音符号[cd]既不区分大小写,也不区分发音符号。 predicate = [NSPredicate predicateWithFormat:@"key contains[c] 'aN'"]; result = [predicate evaluateWithObject:dic];//result = YES predicate = [NSPredicate predicateWithFormat:@"key contains 'hh'"]; result = [predicate evaluateWithObject:dic];//result = NO predicate = [NSPredicate predicateWithFormat:@"key beginswith 'zha'"]; result = [predicate evaluateWithObject:dic];//result = YES predicate = [NSPredicate predicateWithFormat:@"key beginswith 'ha'"]; result = [predicate evaluateWithObject:dic];//result = NO predicate = [NSPredicate predicateWithFormat:@"key endswith 'ang'"]; result = [predicate evaluateWithObject:dic];//result = YES predicate = [NSPredicate predicateWithFormat:@"key endswith 'an'"]; result = [predicate evaluateWithObject:dic];//result = NO predicate = [NSPredicate predicateWithFormat:@"key like '*an*'"]; result = [predicate evaluateWithObject:dic];//result = YES注意:NSPredicate对象中的formate字符串中的key必须是需要判断对象的一个key。
3.数组
根据数组中对象的类型,选用以上两种
0 0
- iOS中NSPredicate的使用方法
- ios中NSPredicate的用法
- IOS中NSPredicate的用法
- ios中NSPredicate的用法
- ios中NSPredicate的用法
- IOS中NSPredicate的用法
- IOS中NSPredicate的用法
- iOS中正则表达式的使用--NSPredicate
- iOS中正则表达式的使用--NSPredicate
- iOS中正则表达式的使用--NSPredicate
- iOS中正则表达式的使用--NSPredicate
- iOS中 NSPredicate 的简单使用
- iOS中关于NSPredicate的使用
- iOS NSPredicate的用法
- NSPredicate 使用方法
- ios中NSPredicate的用法(正则表达式使用)
- iOS NSPredicate类的使用
- iOS NSPredicate的学习讲解
- ListView多选操作模式详解CHOICE_MODE_MULTIPLE与CHOICE_MODE_MULTIPLE_MODAL
- 关于坛友的一个布局问题的解答
- 第十一周(四)贪财的富翁
- DOM获取指定的所有元素
- 函数版星号图
- iOS中NSPredicate的使用方法
- 凡事不要只看表面,不可轻易论断他人。
- Ubuntu14在虚拟机下的安装
- selenium 录制 result is null 问题
- Android逗比自学webservice(三)Axis2
- Log4 日志级别
- 【学习笔记----数据结构12-树、森林与二叉树的转换】
- 未能导入activex控件,请确保它正确注册
- win7 64位无法安装网络打印机驱动