NSPredicate Cheatsheet, Basic, compound, Aggregate, String, comparison operators

 Format string summary

@"attributeName == %@"

object’s attributeName value is equal to value passed in

@"%K == %@"

pass a string variable to %K, it will be represented as a keypath, then check if it’s value is equal to value passed in

@"%name IN $NAME_LIST"

templated for predicate, checks if the value of key name is in $NAME_LIST. Uses predicateWithSubstitutionVariables

@"'name' IN $NAME_LIST"

checks if the constant value ‘name’ is in $NAME_LIST. Uses predicateWithSubstitutionVariables

[NSPredicate predicateWithFormat: @"title == %@", @"minecraft"]



String comparison operators

BEGINSWITH

Left hand expression begins with the right hand expression

CONTAINS

Left hand expression contains the right hand expression

ENDSWITH

Left hand expression ends with the right hand expression

LIKE

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

Left hand expression equals the right hand expression using a regex - style comparison

[NSPredicate predicateWithFormat: @"name BEGINSWITH 'm'"]

Basic comparisons

=,==

Left hand expression is equal to right hand expression

>=,=>

Left hand expression is greater than or equal to right hand expression

<=,=<

Left hand expression is less than or equal to right hand expression

>

Left hand expression is greater than right hand expression

<

Left hand expression is less than right hand expression

!=,<>

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. name IN {‘Milk’, ‘Eggs’, ‘Bread’}

BETWEEN

Left hand expression is between or equal to right hand expression. i.e. 1 Between {0, 33}. If your left hand expression was 0 or 33 it would also make this true

[NSPredicate predicateWithFormat: @"expenses BETWEEN {200, 400}"]


Basic compound predicates

AND,&&

Logical AND

OR,||

Logical OR

NOT,!

Logical NOT

[NSPredicate predicateWithFormat: @"age == 40 AND price > 67"]

Aggregate operators

ANY,SOME

returns objects where ANY or SOME of the predicate results are true.

ALL

returns objects where ALL of the predicate results are true.

NONE

returns objects where NONE of the predicate results are true.

[NSPredicate predicateWithFormat: @"ALL expenses > 1000"]

Keypath collection queries

@avg

returns the average of the objects in the collection as an NSNumber

@count

returns the number of objects in a collection as an NSNumber

@min

returns the minimum value of the objects in the collection as an NSNumber

@max

returns the maximum value of the objects in the collection as an NSNumber

@sum

returns the sum of the objects in the collection based on the property

[NSPredicate predicateWithFormat: @"expenses.@avg.doubleValue < 200"]



Object, array, and set operators

@distinctUnionOfObjects

returns an array containing the distinct objects in the property specified by the key path to the right of the operator

@unionOfObjects

returns the same as @distinctUnionOfObects except it also includes duplicates

NSArray *payees = [transactions valueForKeyPath:@"@distinctUnionOfObjects.payee"]

@distinctUnionOfArrays

returns an array containing the distinct objects in the property specified by the key path to the right of the operator

@unionOfArrays

returns the same as @distinctUnionOfArrays except it also includes duplicates

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"]

@distinctUnionOfSets

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


Array operations

array[index]

specifies the element at the specified index in the array.

array[FIRST]

specifies the first element in the array.

array[LAST]

specifies the last element in the array.

array[SIZE]

specifies the size of the array.

Let’s say we have a person with many dogs. index should be replaced with a number which will return the dog that you want to check against. 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(collection, variableName, predicateFormat)

Iterates through the collection to return qualifying queries

Collection - array or set of objects

variableName - variable that represents an iterated object

predicateFormat - predicate that runs using the variableName

[NSPredicate predicateWithFormat: @"SUBQUERY(tasks, $task, $task.completionDate != nil AND $task.user = 'Alex') .@count > 0"]

Assume this was run on an array of projects. It will return projects with tasks that were not completed by user Alex
























Comments

Popular posts from this blog

what is appDelegate?

CRUD Operation Using RealmSwift database Part 1