iOS中NSPredicate的使用方法

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.数组

根据数组中对象的类型,选用以上两种



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值