The predicate

First, we can look at the official documentation

Definition of a logical condition that constrains a search condition for lookup or in-memory filtering. Personal understanding is a condition used to match the results of a query.

NSPredicate的API

// create predicateWithFormat by expression + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat,... ; // %@ from arguments + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat argumentArray:(nullable NSArray  *)arguments; // the %@ placeholder in the expression will take the value from argList + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat arguments:(va_list)argList; // Create a predicate instance from a metadata query string, Macos only can use the + (nullable NSPredicate *) predicateFromMetadataQueryString: (nsstrings *) the queryString API_AVAILABLE (macos (10.9))  API_UNAVAILABLE(ios, watchos, tvos); // create a predicate instance with fixed BOOL + (NSPredicate *)predicateWithValue:(BOOL)value; // predicateWithBlock:(BOOL (^)(id _Nullable evaluatedObject, NSDictionary<NSString *, ID > * _Nullable Bindings))block API_AVAILABLE(MACOS (10.6), ios(4.0), Watchos (2.0), TVOs (9.0)); @property (readOnly, copy) NSString *predicateFormat; // Replace variables with constant values, With the key values in the dictionary to replace variables declared with $- (instancetype) predicateWithSubstitutionVariables: (NSDictionary < nsstrings *, id > *) variables; EvaluateWithObject (nullable id)object; // substitute constant values for variables EvaluateWithObject :(nullable id)object substitutionVariables:(nullable NSDictionary<NSString *, Id > *) Bindings API_AVAILABLE(MACOS (10.5), ios(3.0), Watchos (2.0), TVOS (9.0)); // Force use of securely decoded predicates for evaluation - (void)allowEvaluation API_AVAILABLE(MacOS (10.9), ios(7.0), watchos(2.0), tVOs (9.0));Copy the code

Commonly used method

As you can see from the official API exposed above, creating an NSPredicate basically requires an expression

Create a predicate

NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"SELF CONTAINS 'world'"]; NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", @"world"]; NSPredicate *predicate3 = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@" argumentArray:@[@"world"]]; NSLog (@ predicate1 predicate expression: "% @", predicate1. PredicateFormat); NSLog (@ predicate2 predicate expression: "% @", predicate2. PredicateFormat); NSLog (@ predicate3 predicate expression: "% @", predicate3. PredicateFormat);Copy the code

The printed result is:

Create the predicate and use it

NSString *str = @"Hello, world"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS 'world'"]; NSLog(@" predicate evaluation result :% HHD ",[predicate evaluateWithObject: STR]);Copy the code

The result is printed as: predicate evaluation result :1

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS $variable"]; predicate = [predicate predicateWithSubstitutionVariables:@{@"variable":@"world"}]; NSLog(@" predicate expression %@", [predicate predicateFormat]);Copy the code
NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSArray * _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {// Filter if ([evaluatedObject) or bindings ([evaluatedObject) containsObject:[bindings objectForKey:@"key"]]) { return YES; } else { return NO; } }]; BOOL result = [predicate evaluateWithObject:@[@"1", @"2"] substitutionVariables:@{@"key":@"2"}]; NSLog(@" predicate evaluation result % HHD ", result);Copy the code

The expansion of the NSPredicate

@interface NSArray<ObjectType> (NSPredicateSupport) The returned array - (NSArray < ObjectType > *) filteredArrayUsingPredicate (NSPredicate *) predicate; @end@interface NSMutableArray<ObjectType> (NSPredicateSupport) // Remove all array elements that do not qualify for predicate evaluation - (void)filterUsingPredicate:(NSPredicate *)predicate; @endCopy the code

NSSet related classes have a similar method, just like arrays.

Predicate expression syntax

You can add [CD] after the keyword. C indicates that case is ignored, that is, CAFE and CAFE are the same string. D indicates that the pronunciation is ignored, that is, cafe and cafe are the same string.

Common literal semantics

expression use
SELF The object that the predicate evaluates
‘text’ The string text, in double quotes, requires the escape character \
TRUE, YES Logic is
FALSE, NO Logical false
A placeholder
expression use
%K The property name
% @ Attribute values
NSString *name = @"name"; NSString *value = @"value"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K CONTAINS %@", name, value]; NSLog(@" predicate expression %@", [predicate predicateFormat]); BOOL result = [predicate evaluateWithObject:@{@"name" : @"value111"}]; NSLog(@" predicate result is % HHD ", result);Copy the code

Outputs: predicate expression name CONTAINS “value”

The predicate result is 1

Elementary comparison operation

expression use
=, = = Is equal to the
> =, = > Greater than or equal to
< =, = < Less than or equal to
> Is greater than
< Less than
! =, < > Is not equal to
BETWEEN Values within the interval (including boundary values)

Here is an example of BETWEEN

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF BETWEEN {200, 250}"]; NSLog(@" predicate evaluation result :% HHD ", [predicate evaluateWithObject:@(212)]); // Predicate evaluation result :1Copy the code

Boolean value

expression use
TRUEPREDICATE true
FALSEPREDICATE false

The expression for [NSPredicate predicateWithValue:YES] is actually the TRUEPREDICATE

Logical operations
expression use
AND, && with
OR, | | or
NOT、! non
String comparison
expression use
BEGINSWITH The string begins with… start
ENDSWITH The string begins with… The end of the
CONTAINS The string contains…
LIKE String equality
MATCHES The string matches the regular expression on the right
Set operations
expression use
ANY, SOME, Whether at least one element in the set satisfies the condition
ALL All the elements in the set satisfy this condition
NONE None of the elements in the set satisfy this condition
IN Contained by the set
SELF[index] Take the element of the corresponding index
SELF[FIRST] The first element
SELF[LAST] The last element
SELF[SIZE] Number of elements
SELF[‘key’] or self. key or key The dictionary corresponds to the element of the key

Reference Link 1

Reference Link 2

If there are any mistakes, please correct them

IOS development QQ communication group: 568571839, welcome to tease ~