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.
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
.
-
The left-hand expression begins with the right-hand expression.
-
The left-hand expression contains the right-hand expression.
-
The left-hand expression ends with the right-hand expression.
-
The left hand expression equals the right-hand expression:
?
and*
are allowed as wildcard characters, where?
matches1
character and*
matches0
or more characters. -
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).
BEGINSWITH
CONTAINS
ENDSWITH
LIKE
MATCHES
Aggregate Operations
-
Specifies any of the elements in the following expression. For example
ANY children.age < 18
. -
Specifies all of the elements in the following expression. For example
ALL children.age < 18
. -
Specifies none of the elements in the following expression. For example,
NONE children.age < 18
. This is logically equivalent toNOT (ANY ...)
. -
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 ofNSArray
,NSSet
,NSDictionary
, or of any of the corresponding mutable classes. -
Specifies the element at the specified index in the array
array
. -
Specifies the first element in the array
array
. -
Specifies the last element in the array
array
. -
Specifies the size of the array
array
.
ANY
, SOME
ALL
NONE
IN
array[index]
array[FIRST]
array[LAST]
array[SIZE]