NSPredicate 用法笔记

1. 两种类型  comparison 比较类型 ( = ,<  ) componud 结合类型(AND OR  NOT )。 

2. simple comparisons   Age == 30 ,first name like "adam"  ; 

    case or diacritic insensitive lookups  : name contains[cd] 'adam' 

    logical operations :  first name beginwith "w"AND lastName like 'tom'

3. 案例 nspredicate *predicate = [nspredicate predicateWithFormat :@"(lastName like (cd) %@") AND (birthday > %@))" nameStr , birthday];

4 返回boolean  evalutateWithObject 


5.coreData with nspredicate

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Employee"
        inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
 
NSNumber *salaryLimit = <#A number representing the limit#>;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"salary > %@", salaryLimit];
[request setPredicate:predicate];
NSError *error;
NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];

  • %@ is a var arg substitution for an object value—often a string, number, or date.

  • %K is a var arg substitution for a key path.

6. 字符串查询

String Comparisons

String comparisons are, by default, case and diacritic sensitive. You can modify an operator using the key characters c and d within square braces to specify case and diacritic insensitivity respectively, for example firstName BEGINSWITH[cd] $FIRST_NAME.

BEGINSWITH

The left-hand expression begins with the right-hand expression.

CONTAINS

The left-hand expression contains the right-hand expression.

ENDSWITH

The left-hand expression ends with the right-hand expression.

LIKE

The left hand expression equals the right-hand expression: ? and * are allowed as wildcard characters, where ? matches 1 character and * matches 0 or more characters.

MATCHES

The left hand expression equals the right hand expression using a regex-style comparison according to ICU v3 (for more details see the ICU User Guide for Regular Expressions).

Aggregate Operations

ANYSOME

Specifies any of the elements in the following expression. For example ANY children.age < 18

ALL

Specifies all of the elements in the following expression. For example ALL children.age < 18.

NONE

Specifies none of the elements in the following expression. For example,  NONE children.age < 18. This is logically equivalent to NOT (ANY ...).

IN

Equivalent to an SQL IN operation, the left-hand side must appear in the collection specified by the right-hand side.

For example, name IN { 'Ben', 'Melissa', 'Nick' }. The collection may be an array, a set, or a dictionary—in the case of a dictionary, its values are used.

In Objective-C, you could create a IN predicate as shown in the following example:

NSPredicate *inPredicate =
            [NSPredicate predicateWithFormat: @"attribute IN %@", aCollection];

where aCollection may be an instance of NSArrayNSSetNSDictionary, or of any of the corresponding mutable classes.

array[index]

Specifies the element at the specified index in the array array

array[FIRST]

Specifies the first element in the array array

array[LAST]

Specifies the last element in the array array

array[SIZE]

Specifies the size of the array array.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值