Format String Summary
Attribute name Object's @"attributeName == %@" |
|
Keypath Pass a string variable to "%K == %@" |
|
Templated for predicates Templated for predicate, checks if the value of key name is in @"%name IN $NAME_LIST" |
|
Substitution predicate Checks if the constant value @"'name' IN $NAME_LIST" |
|
Example [NSPredicate predicateWithFormat: @"title == %@", @"minecraft"] |
Basic Comparisons
Equal Left hand expression is equal to right hand expression =,== |
|
Grater or equal Left hand expression is greater than or equal to right hand expression >=,=> |
|
Lesser or equal Left hand expression is less than or equal to right hand expression <=,=< |
|
Greater Left hand expression is greater than right hand expression > |
|
Lesser Left hand expression is less than right hand expression < |
|
Different Left hand expression is not equal to right hand expression !=,<> |
|
In Left hand expression must appear in collection specified by right hand expression, i.e. IN |
|
Between Left hand expression is between or equal to right hand expression, i.e. BETWEEN |
|
Example [NSPredicate predicateWithFormat: @"expenses BETWEEN {200, 400}"] |
Keypath Collection Queries
Average Returns the average of the objects in the collection as an NSNumber @avg |
|
Count Returns the number of objects in a collection as an NSNumber @count |
|
Min Returns the minimum value of the objects in the collection as an NSNumber @min |
|
Max Returns the maximum value of the objects in the collection as an NSNumber @max |
|
Sum Returns the sum of the objects in the collection based on the property @sum |
|
Example [NSPredicate predicateWithFormat: @"expenses.@avg.doubleValue < 200"] |
Basic Compound Predicates
AND Logical AND AND,&& |
|
OR Logical OR OR,|| |
|
NOT Logical NOT NOT,! |
|
Example [NSPredicate predicateWithFormat: @"age == 40 AND price > 67"] |
Object, Array, and Set Operators
Distinct union of objects Returns an array containing the distinct objects in the property specified by the key path to the right of the operator @distinctUnionOfObjects |
|
Union of objects Returns the same as @unionOfObjects |
|
Example NSArray *payees = [transactions valueForKeyPath:@"@distinctUnionOfObjects.payee"] |
|
Distinct union of arrays Returns an array containing the distinct objects in the property specified by the key path to the right of the operator @distinctUnionOfArrays |
|
Union of arrays Returns the same as NOT,! |
|
Example These must be run on an array of arrays. For example if you had: NSArray *arrayOfTransactions = [[Array of transactions], [Array of transactions]] NSArray *payees = [arrayOfTransactions valueForKeyPath:@"@distinctUnionOfObjects.payee"] |
|
Distinct union of sets Returns an NSSet instance containing distinct objects in the property specified by the key path to the right of the operator. Expects an NSSet instance containing NSSet instances @distinctUnionOfSets |
String Comparison Operators
Begins with Left hand expression begins with the right hand expression BEGINSWITH |
|
Contains Left hand expression contains the right hand expression CONTAINS |
|
Ends with Left hand expression ends with the right hand expression ENDSWITH |
|
Like Left hand expression equals the right hand expression: LIKE |
|
Matches Left hand expression equals the right hand expression using a regex - style comparison MATCHES |
|
Example [NSPredicate predicateWithFormat: @"name BEGINSWITH 'm'"] |
Aggregate Operators
Some, any Returns objects where ANY,SOME |
|
All Returns objects where ALL |
|
None Returns objects where NONE |
|
Example [NSPredicate predicateWithFormat: @"ALL expenses > 1000"] |
Array Operations
Array index Specifies the element at the specified index in the array array[index] |
|
Array first Specifies the first element in the array array[FIRST] |
|
Array last Specifies the last element in the array array[LAST] |
|
Array size Specifies the size of the array array[Size] |
|
Example Let's say we have a person with many dogs. // Here we're checking if the first dog's age is 5. [NSPredicate predicateWithFormat: @"dogs[0].age = 5"] // Here we're checking if a person has 3 dogs [NSPredicate predicateWithFormat: @"dogs[SIZE] = 3"] |
Subqueries
Subquery Iterates through the collection to return qualifying queries
SUBQUERY(collection, variableName, predicateFormat) |
|
Example Assume this was run on an array of projects. It will return projects with tasks that were not completed by user Alex [NSPredicate predicateWithFormat: @"SUBQUERY(tasks, $task, $task.completionDate != nil AND $task.user = 'Alex') .@count > 0"] |
Tip, Tricks and Examples
Common mistakes
|
|
Using self When using a predicate on an array, NSPredicate *billingPredicate = [NSPredicate predicateWithFormat: @"SELF IN %@", addressesThatOweWaterBill] NSArray *myApartmentsThatOweWaterBill = [myApartmentAddresses filteredArrayUsingPredicate:billingPredicate] |
|
LIKE wildcard match with * and ?
@[@"Sarah", @"Silva", @"silva", @"Silvy", @"Silvia", @"Si*"] predicateWithFormat: @"SELF == %@", @"Sarah" // Will return “Sarah” predicateWithFormat: @"SELF LIKE[c] %@", "Si*" // Will return “Silva”, “silva”, “Silvy”, “Silvia”, “Si*”
predicateWithFormat: @"SELF LIKE[c] %@", "Silv?" // Will return “Silva”, “silva”, “Silvy” |
|
Quick tips
Make sure your columns are indexed to improve performance of using IN operators
predicateWithFormat: @"name CONTAINS[c] 'f'" |
|
Keypath collection queries Keypath collection queries work best when you work with a lot of numbers. Being able to call the min or max, adding things up, and then filtering results are simpler when you only have to append an extra parameter. By having an array of expenses, you can do a quick check on if something is below or above a range of allowed expenses. [NSPredicate predicateWithFormat: @"expenses.@avg.doubleValue < 200"] |
|
How subqueries work SUBQUERY(collection, variableName, predicate) A subquery takes a collection then iterates through each object (as predicateWithFormat: @"SUBQUERY(tasks, $task, $task.completionDate != nil AND $task.user = 'Alex') .@count > 0"
|
Notes
- Based on a cheat sheet by Realm