Nspredicate Cheat Sheet Page 2

ADVERTISEMENT

Tips, Tricks, & Examples
Quick tips
Common mistakes
CFStringTransform normalizes strings if diacritic insensitive isn’t
Using stringWithFormat: to build predicates, prone to have weird
enough. For example you could turn Japanese characters into a
non escaped diacritic characters and artifacts like an apostrophe.
Latin alphabetic representation. It’s extremely powerful with a lot
Use NSPredicate predicateWithFormat instead.
of methods that you can see here:
Using OR OR OR instead of IN, results in repeatable code and can
cfstringtransform/
be less efficient
Make sure your columns are indexed to improve performance of
using IN operators
When using REGEX and Matches, make sure they are the last part
of your predicate statment so it does less work.
[c] case insensitive: lower & uppercase values are treated the
same
[d] diacritic insensitive: special characters treated as the base
Using SELF
character
predicateWithFormat: @"name CONTAINS[c] ' f ' "
When using a predicate on an array, SELF refers to each object in
the array. Here’s an example: Imagine you are a landlord figuring
out which apartments have to pay their water bill. If you have a
Keypath collection queries
list of all the city wide apartment’s that still need to pay called
addressesThatOweWaterBill, we can check that against our
Keypath collection queries work best when you work with a lot of
owned apartments, myApartmentAddresses.
numbers. Being able to easily call the min or max, adding things
NSPredicate *billingPredicate = [NSPredicate
up, and then filtering results can be much simpler when you only
predicateWithFormat: @"SELF IN %@",
have to append an extra parameter. By having an array of
addressesThatOweWaterBill]
expenses, you can easily do a quick check on if something is
below or above a range of allowed expenses.
NSArray *myApartmentsThatOweWaterBill =
[myApartmentAddresses
predicateWithFormat: @"expenses.@avg.doubleValue < 200"
filteredArrayUsingPredicate:billingPredicate]
How Subqueries work
LIKE wildcard match with * and ?
SUBQUERY(collection, variableName, predicate)
* matches 0 or more characters. For example:
Let’s say we have an array of names we want to filter
A subquery takes a collection then iterates through each object
(as variableName) checking the predicate against that object
@["Sarah", "Silva", "silva", "Silvy", "Silvia", Si*"]
(variableName). It works well if you have a collection (A) objects,
[NSPredicate predicateWithFormat: @"SELF ==
and each object has a collection (B) other objects. If you’re trying
%@", "Sarah"]
to filter A based on 2 or more varying attributes of B.
Will return “Sarah”
[NSPredicate predicateWithFormat: @"SUBQUERY(tasks,
[NSPredicate predicateWithFormat: @"SELF LIKE[c]
$task, $task.completionDate != nil AND $task.user =
%@", "Si*"]
'Alex').@count > 0"]
Will return “Silva”, “silva”, “Silvy”, “Silvia”, “Si*”
SUBQUERY(…) returns an array. We need to check if its count > 0
to return the true or false the predicate expects.
? matches 1 character only
[NSPredicate predicateWithFormat: @"SELF LIKE[c] %@",
"Silv?"]
Will return “Silva”, “silva”, “Silvy”

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 2