Nspredicate Cheat Sheet

ADVERTISEMENT

NSPredicate Cheatsheet
Basic Comparisons
Presented by Realm: a mobile database that replaces
=,==
Left hand expression is equal to right hand expression
Core Data and SQLite. Learn more at
Left hand expression is greater than or equal to right
>=,=>
supported by Realm
hand expression
Left hand expression is less than or equal to right hand
<=,=<
expression
Format String Summary
>
Left hand expression is greater than right hand expression
<
Left hand expression is less than right hand expression
@"attributeName == %@"
object’s attribute name is equal to value passed in
!=,<>
Left hand expression is not equal to right hand expression
@"%K == %@"
Left hand expression must appear in collection specified by
pass a string variable to %K, it will be represented as a
IN
right hand expression. i.e. name IN {‘Milk’, ‘Eggs’, ‘Bread’}
keypath
Left hand expression is between or equal to right hand
Left hand expression is between or equal to right hand
BETWEEN
BETWEEN
@"%name IN $NAME_LIST"
expression. i.e. 1 Between {0, 33}
expression. i.e. 1 Between {0, 33}
templated for predicate, checks if the value of key name is
predicateWithFormat: @"expenses BETWEEN {200, 400}"
in $NAME_LIST. Uses predicateWithSubstitutionVariables
Basic Compound Predicates
@"'name' IN $NAME_LIST"
checks if the constant value ‘name’ is in $NAME_LIST. Uses
predicateWithSubstitutionVariables
Logical AND
AND,&&
predicateWithFormat: @"title == %@", @"minecraft"
Logical OR
OR,||
Logical NOT
NOT,!
Keypath collection queries
predicateWithFormat: @"age == 40 AND price > 67"
returns the average of the objects in the
@avg
String Comparison Operators
collection as an NSNumber
returns the number of objects in a collection as
@count
Left hand expression begins with the right hand
an NSNumber
BEGINS WITH
expression
returns the minimum value of the objects in the
@min
collection as an NSNumber
Left hand expression contains the right hand
CONTAINS
expression
returns the maximum value of the objects in the
@max
collection as an NSNumber
Left hand expression ends with the right hand
returns the sum of the objects in the collection
ENDSWITH
expression
@sum
based on the property
predicateWithFormat:
Left hand expression equals the right hand
@"expenses.@avg.doubleValue < 200"
expression: ? and * are allowed as wildcard
LIKE
characters, where ? matches 1 character and *
matches 0 or more characters
Subqueries
Left hand expression equals the right hand
MATCHES
expression using a regex - style comparison
SUBQUERY(collection, variableName, predicateFormat)
predicateWithFormat: @"name BEGINS WITH 'm'"
Iterates through the collection to return qualifying queries
Aggregate Operators
Collection - array or set of objects
variableName - variable that represents an iterated object
returns objects where ANY or SOME of the predicate
predicateFormat - predicate that runs using the
ANY,SOME
results are true.
variableName
returns objects where ALL of the predicate results are
ALL
predicateWithFormat: @"SUBQUERY(tasks, $task,
true.
$task.completionDate != nil AND $task.user = 'Alex')
returns objects where NONE of the predicate results
.@count > 0"
NONE
are true.
Assume this was run on an array of projects. It will return
projects with tasks that were not completed by user Alex
predicateWithFormat:@"ALL expenses > 1000"

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2