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 or firstName 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
原创粉丝点击