Showing posts with label NSComparisonPredicate. Show all posts
Showing posts with label NSComparisonPredicate. Show all posts

Friday, June 14, 2013

NSComparisonPredicate NSLikePredicateOperatorType example in Objective C (iOS).


NSComparisonPredicate NSLikePredicateOperatorType

NSPredicateOperatorType
Defines the type of comparison for NSComparisonPredicate.

enum {
NSLessThanPredicateOperatorType = 0,
NSLessThanOrEqualToPredicateOperatorType,
NSGreaterThanPredicateOperatorType,
NSGreaterThanOrEqualToPredicateOperatorType,
NSEqualToPredicateOperatorType,
NSNotEqualToPredicateOperatorType,
NSMatchesPredicateOperatorType,
NSLikePredicateOperatorType,
NSBeginsWithPredicateOperatorType,
NSEndsWithPredicateOperatorType,
NSInPredicateOperatorType,
NSCustomSelectorPredicateOperatorType,
NSContainsPredicateOperatorType,
NSBetweenPredicateOperatorType
};
typedef NSUInteger NSPredicateOperatorType;

Constants
NSLessThanPredicateOperatorType
A less-than predicate.
NSLessThanOrEqualToPredicateOperatorType
A less-than-or-equal-to predicate.
NSGreaterThanPredicateOperatorType
A greater-than predicate.
NSGreaterThanOrEqualToPredicateOperatorType
A greater-than-or-equal-to predicate.
NSEqualToPredicateOperatorType
An equal-to predicate.
NSNotEqualToPredicateOperatorType
A not-equal-to predicate.
NSMatchesPredicateOperatorType
A full regular expression matching predicate.
NSLikePredicateOperatorType
A simple subset of the MATCHES predicate, similar in behavior to SQL LIKE.
NSBeginsWithPredicateOperatorType
A begins-with predicate.
NSEndsWithPredicateOperatorType
An ends-with predicate.
NSInPredicateOperatorType
A predicate to determine if the left hand side is in the right hand side.
For strings, returns YES if the left hand side is a substring of the right hand side . For collections, returns YES if the left hand side is in the right hand side .
NSCustomSelectorPredicateOperatorType
A predicate that uses a custom selector that takes a single argument and returns a BOOL value.
The selector is invoked on the left hand side with the right hand side as the argument.
NSContainsPredicateOperatorType
A predicate to determine if the left hand side contains the right hand side.
Returns YES if [lhs contains rhs]; the left hand side must be an NSExpression object that evaluates to a collection
NSBetweenPredicateOperatorType
A predicate to determine if the right hand side lies at or between bounds specified by the left hand side.
Returns YES if [lhs between rhs]; the right hand side must be an array in which the first element sets the lower bound and the second element the upper, inclusive. Comparison is performed using compare: or the class-appropriate equivalent.

NSComparisonPredicate NSLikePredicateOperatorType example.
NSExpression *nameExpression = [NSExpression expressionForKeyPath:@"name"];
NSArray *operators = [NSArray arrayWithObjects:
      [NSNumber numberWithInt: NSEqualToPredicateOperatorType],
      [NSNumber numberWithInt:NSNotEqualToPredicateOperatorType],
      [NSNumber numberWithInt:NSLikePredicateOperatorType],
      [NSNumber numberWithInt:NSBeginsWithPredicateOperatorType],
      [NSNumber numberWithInt:NSEndsWithPredicateOperatorType],
      [NSNumber numberWithInt:NSContainsPredicateOperatorType],
      nil];

NSUInteger options = (NSCaseInsensitivePredicateOption |
                      NSDiacriticInsensitivePredicateOption);

NSPredicateEditorRowTemplate *template = [[NSPredicateEditorRowTemplate alloc]
      initWithLeftExpressions:[NSArray arrayWithObject:nameExpression]
      rightExpressionAttributeType:NSStringAttributeType
      modifier:NSDirectPredicateModifier
      operators:operators
      options:options];

Example of [NSComparisonPredicate NSLikePredicateOperatorType].
-(NSPredicate *) predicateWithStudyID:(NSString *) aStudyID studyName:(NSString *) aStudyName date:(NSDate *) aDate{
  NSPredicate *p;
  NSMutableArray *preds=[[NSMutableArray alloc] initWithCapacity:1];
  if (aDate != nil) {
    NSExpression *dateKeyEx=[NSExpression expressionForKeyPath:@"studyDate"];
    NSExpression *aDateEx=[NSExpression expressionForConstantValue:aDate];
    NSPredicate *datePred=[NSComparisonPredicate predicateWithLeftExpression:dateKeyEx
                                                             rightExpression:aDateEx
                                                                    modifier:NSDirectPredicateModifier
                                                                        type:NSEqualToPredicateOperatorType
                                                                     options:0];
    [preds addObject:datePred];
  }
  if (![aStudyID isEqualToString:@""]) {
    NSExpression *studyIDKeyEx=[NSExpression expressionForKeyPath:@"studyID"];
    NSExpression *aStudyIDEx=[NSExpression expressionForConstantValue:aStudyID];
    NSPredicate *studyIDPred=[NSComparisonPredicate predicateWithLeftExpression:studyIDKeyEx
                                                                rightExpression:aStudyIDEx
                                                                       modifier:NSDirectPredicateModifier
                                                                           type:NSLikePredicateOperatorType
                                                                        options:NSCaseInsensitivePredicateOption];
    [preds addObject:studyIDPred];
  }
  if (![aStudyName isEqualToString:@""]) {
    NSExpression *studyNameKeyEx=[NSExpression expressionForKeyPath:@"studyName"];
    NSExpression *aStudyNameEx=[NSExpression expressionForConstantValue:aStudyName];
    NSPredicate *studyNamePred=[NSComparisonPredicate predicateWithLeftExpression:studyNameKeyEx
                                                                rightExpression:aStudyNameEx
                                                                       modifier:NSDirectPredicateModifier
                                                                           type:NSLikePredicateOperatorType
                                                                        options:NSCaseInsensitivePredicateOption];
    [preds addObject:studyNamePred];
  }
  p=[NSCompoundPredicate andPredicateWithSubpredicates:preds]; 
  [preds release];
  return p;
}

NSComparisonPredicate NSLikePredicateOperatorType example.
NSPredicate *nameQuery =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_name"]
                                   rightExpression:[NSExpression expressionForConstantValue:Name]
                                          modifier:NSDirectPredicateModifier
                                              type:NSLikePredicateOperatorType
                                           options:NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption];

NSPredicate *regNoQuery =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_regno"]
                                   rightExpression:[NSExpression expressionForConstantValue:RegNo]
                                          modifier:NSDirectPredicateModifier
                                              type:NSEqualToPredicateOperatorType
                                           options:0];

NSPredicate *query = [NSCompoundPredicate andPredicateWithSubpredicates:@[nameQuery,regNoQuery]];

End of NSComparisonPredicate NSLikePredicateOperatorType example article.

NSComparisonPredicate NSGreaterThanPredicateOperatorType example in Objective C (iOS).


NSComparisonPredicate NSGreaterThanPredicateOperatorType

NSPredicateOperatorType
Defines the type of comparison for NSComparisonPredicate.

enum {
NSLessThanPredicateOperatorType = 0,
NSLessThanOrEqualToPredicateOperatorType,
NSGreaterThanPredicateOperatorType,
NSGreaterThanOrEqualToPredicateOperatorType,
NSEqualToPredicateOperatorType,
NSNotEqualToPredicateOperatorType,
NSMatchesPredicateOperatorType,
NSLikePredicateOperatorType,
NSBeginsWithPredicateOperatorType,
NSEndsWithPredicateOperatorType,
NSInPredicateOperatorType,
NSCustomSelectorPredicateOperatorType,
NSContainsPredicateOperatorType,
NSBetweenPredicateOperatorType
};
typedef NSUInteger NSPredicateOperatorType;

Constants
NSLessThanPredicateOperatorType
A less-than predicate.
NSLessThanOrEqualToPredicateOperatorType
A less-than-or-equal-to predicate.
NSGreaterThanPredicateOperatorType
A greater-than predicate.
NSGreaterThanOrEqualToPredicateOperatorType
A greater-than-or-equal-to predicate.
NSEqualToPredicateOperatorType
An equal-to predicate.
NSNotEqualToPredicateOperatorType
A not-equal-to predicate.
NSMatchesPredicateOperatorType
A full regular expression matching predicate.
NSLikePredicateOperatorType
A simple subset of the MATCHES predicate, similar in behavior to SQL LIKE.
NSBeginsWithPredicateOperatorType
A begins-with predicate.
NSEndsWithPredicateOperatorType
An ends-with predicate.
NSInPredicateOperatorType
A predicate to determine if the left hand side is in the right hand side.
For strings, returns YES if the left hand side is a substring of the right hand side . For collections, returns YES if the left hand side is in the right hand side .
NSCustomSelectorPredicateOperatorType
A predicate that uses a custom selector that takes a single argument and returns a BOOL value.
The selector is invoked on the left hand side with the right hand side as the argument.
NSContainsPredicateOperatorType
A predicate to determine if the left hand side contains the right hand side.
Returns YES if [lhs contains rhs]; the left hand side must be an NSExpression object that evaluates to a collection
NSBetweenPredicateOperatorType
A predicate to determine if the right hand side lies at or between bounds specified by the left hand side.
Returns YES if [lhs between rhs]; the right hand side must be an array in which the first element sets the lower bound and the second element the upper, inclusive. Comparison is performed using compare: or the class-appropriate equivalent.

NSComparisonPredicate NSGreaterThanPredicateOperatorType example.
// First part: name != <varName>
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"name != %@", varName];

// Second part: date <op> <varDate>
NSExpression *lexp = [NSExpression expressionForKeyPath:@"date"];
NSExpression *rexp = [NSExpression expressionForConstantValue:varDate];
NSPredicateOperatorType op = (direction == kPageDirectionForward) ? NSGreaterThanPredicateOperatorType : NSLessThanOrEqualToPredicateOperatorType;
NSPredicate *p2 = [NSComparisonPredicate predicateWithLeftExpression:lexp
                                                     rightExpression:rexp
                                                            modifier:0
                                                                type:op
                                                             options:0];
// predicate = <p1> AND <p2>
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2]];

Example of [NSComparisonPredicate NSGreaterThanPredicateOperatorType].
    //Only events newer than 14 days
    NSDate* breakDate = [NSDate dateWithTimeIntervalSinceNow:(-1)*(14*24*60*60)];
    NSPredicate *p = [NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"startDate"]
                                                        rightExpression:[NSExpression expressionForConstantValue:breakDate]
                                                               modifier:NSDirectPredicateModifier
                                                                   type:NSGreaterThanPredicateOperatorType
                                                                options:0];
    [fetchRequest setPredicate:p];

End of NSComparisonPredicate NSGreaterThanPredicateOperatorType example article.

NSComparisonPredicate NSDiacriticInsensitivePredicateOption example in Objective C (iOS).


NSComparisonPredicate NSDiacriticInsensitivePredicateOption

NSComparisonPredicate Options
These constants describe the possible types of string comparison for NSComparisonPredicate. These options are supported for LIKE as well as all of the equality/comparison operators.

enum {
NSCaseInsensitivePredicateOption = 0x01,
NSDiacriticInsensitivePredicateOption = 0x02,
NSNormalizedPredicateOption = 0x04,
NSLocaleSensitivePredicateOption = 0x08
};
typedef NSUInteger NSComparisonPredicateOptions;

Constants
NSCaseInsensitivePredicateOption
A case-insensitive predicate.
You represent this option in a predicate format string using a [c] following a string operation (for example, "NeXT" like[c] "next").
NSDiacriticInsensitivePredicateOption
A diacritic-insensitive predicate.
You represent this option in a predicate format string using a [d] following a string operation (for example, "naïve" like[d] "naive").
NSNormalizedPredicateOption
Indicates that the strings to be compared have been preprocessed.
This option supersedes NSCaseInsensitivePredicateOption and NSDiacriticInsensitivePredicateOption, and is intended as a performance optimization option.
You represent this option in a predicate format string using a [n] following a string operation (for example, "WXYZlan" matches[n] ".lan").
NSLocaleSensitivePredicateOption
Indicates that strings to be compared using <, <=, =, =>, > should be handled in a locale-aware fashion.
You represent this option in a predicate format string using a [l] following one of the <, <=, =, =>, > operators (for example, "straße" >[l] "strasse").

NSComparisonPredicate NSDiacriticInsensitivePredicateOption example.
NSExpression * leftExpression = [NSExpression expressionForKeyPath:@"name"];
NSExpression * rightExpression = [NSExpression expressionForConstantValue:@"Hello!"];

NSComparisonPredicate * predicate = [NSComparisonPredicate predicateWithLeftExpression:leftExpression
                                                                       rightExpression:rightExpression
                                                                              modifier:NSDirectPredicateModifier
                                                                                  type:NSContainsPredicateOperatorType
                                                                               options:(NSCaseInsensitivePredicateOption | NSDiacriticInsensitivePredicateOption)];

Example of [NSComparisonPredicate NSDiacriticInsensitivePredicateOption].
NSPredicate *nameQuery =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_name"]
                                   rightExpression:[NSExpression expressionForConstantValue:Name]
                                          modifier:NSDirectPredicateModifier
                                              type:NSLikePredicateOperatorType
                                           options:NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption];

NSPredicate *regNoQuery =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_regno"]
                                   rightExpression:[NSExpression expressionForConstantValue:RegNo]
                                          modifier:NSDirectPredicateModifier
                                              type:NSEqualToPredicateOperatorType
                                           options:0];

NSPredicate *query = [NSCompoundPredicate andPredicateWithSubpredicates:@[nameQuery,regNoQuery]];

End of NSComparisonPredicate NSDiacriticInsensitivePredicateOption example article.

NSComparisonPredicate leftExpression example in Objective C (iOS).


NSComparisonPredicate leftExpression

Returns the left expression for the receiver.

- (NSExpression *)leftExpression

Return Value of [NSComparisonPredicate leftExpression]
The left expression for the receiver, or nil if there is none.

NSComparisonPredicate leftExpression example.
- (NSPredicate *)predicateWithEnhancedSubstitutionVariables:(NSDictionary *)vars {
  if ([self isKindOfClass:[NSCompoundPredicate class]]) {
    // recurse for compound predicates
    NSCompoundPredicate *compound = (NSCompoundPredicate *)self;
    NSMutableArray *subpredicates = [NSMutableArray array];
    for (NSPredicate *subpredicate in [compound subpredicates]) {
      NSPredicate *new = [subpredicate predicateWithEnhancedSubstitutionVariables:vars];
      [subpredicates addObject:new];
    }
    return [[[NSCompoundPredicate alloc] initWithType:[compound compoundPredicateType]
                                        subpredicates:subpredicates] autorelease];
  } else {
    NSPredicate *final = self;
    // on comparison predicates, pull out the left and right expressions
    NSComparisonPredicate *comparison = (NSComparisonPredicate *)self;
    NSExpression *left = [comparison leftExpression];

    // substitute, if appropriate
    if ([left expressionType] == NSVariableExpressionType) {
      NSString *variable = [left variable];
      if ([variable hasPrefix:@"KEY_"]) {
        NSString *replacement = [vars objectForKey:variable];
        if (replacement) {
          left = [NSExpression expressionForKeyPath:replacement];
        }
      }
    }

    // substitute, if appropriate
    NSExpression *right = [comparison rightExpression];
    if ([right expressionType] == NSVariableExpressionType) {
      NSString *variable = [right variable];
      if ([variable hasPrefix:@"KEY_"]) {
        NSString *replacement = [vars objectForKey:variable];
        if (replacement) {
          right = [NSExpression expressionForKeyPath:replacement];
        }
      }
    }

    // build and return the appropriate comparison predicate
    if (left != [comparison leftExpression] || right != [comparison rightExpression]) {
      if ([comparison customSelector] != NULL) {
        final = [NSComparisonPredicate predicateWithLeftExpression:left
                                                   rightExpression:right
                                                    customSelector:[comparison customSelector]];
      } else {
        final = [NSComparisonPredicate predicateWithLeftExpression:left
                                                   rightExpression:right
                                                          modifier:[comparison comparisonPredicateModifier]
                                                              type:[comparison predicateOperatorType]
                                                           options:[comparison options]];
      }
    }
    return [final predicateWithSubstitutionVariables:vars];
  }
}

Example of [NSComparisonPredicate leftExpression].
NSPredicate * parsed = [NSPredicate predicateWithFormat:@"6 * 7 = 0"];
NSExpression * left = [(NSComparisonPredicate *)parsed leftExpression];
NSNumber * result = [left expressionValueWithObject:nil context:nil];
NSLog(@"result: %@", result);

NSComparisonPredicate leftExpression example.
NSString *equation = @"1+5*6";

// dummy predicate that contains our expression
NSPredicate *pred = [NSPredicate predicateWithFormat:
                      [equation stringByAppendingString:@" == 42"]];
NSExpression *exp = [pred leftExpression];
NSNumber *result = [exp expressionValueWithObject:nil context:nil];
NSLog(@"%@", result); // logs "31"

End of NSComparisonPredicate leftExpression example article.

NSComparisonPredicate initWithLeftExpression rightExpression modifier type options example in Objective C (iOS).


NSComparisonPredicate initWithLeftExpression rightExpression modifier type options

Initializes a predicate to a given type formed by combining given left and right expressions using a given modifier and options.

- (id)initWithLeftExpression:(NSExpression *)lhs rightExpression:(NSExpression *)rhs modifier:(NSComparisonPredicateModifier)modifier type:(NSPredicateOperatorType)type options:(NSUInteger)options

Parameters of [NSComparisonPredicate initWithLeftExpression rightExpression modifier type options]
lhs
The left hand expression.
rhs
The right hand expression.
modifier
The modifier to apply.
type
The predicate operator type.
options
The options to apply (see “NSComparisonPredicate Options”). For no options, pass 0.

Return Value of [NSComparisonPredicate initWithLeftExpression rightExpression modifier type options]
The receiver, initialized to a predicate of type type formed by combining the left and right expressions using the modifier and options.

NSComparisonPredicate initWithLeftExpression rightExpression modifier type options example.
NSExpression *nameExpression = [NSExpression expressionForKeyPath:@"name"];
NSArray *operators = [NSArray arrayWithObjects:
      [NSNumber numberWithInt: NSEqualToPredicateOperatorType],
      [NSNumber numberWithInt:NSNotEqualToPredicateOperatorType],
      [NSNumber numberWithInt:NSLikePredicateOperatorType],
      [NSNumber numberWithInt:NSBeginsWithPredicateOperatorType],
      [NSNumber numberWithInt:NSEndsWithPredicateOperatorType],
      [NSNumber numberWithInt:NSContainsPredicateOperatorType],
      nil];

NSUInteger options = (NSCaseInsensitivePredicateOption |
                      NSDiacriticInsensitivePredicateOption);

NSPredicateEditorRowTemplate *template = [[NSPredicateEditorRowTemplate alloc]
      initWithLeftExpressions:[NSArray arrayWithObject:nameExpression]
      rightExpressionAttributeType:NSStringAttributeType
      modifier:NSDirectPredicateModifier
      operators:operators
      options:options];

End of NSComparisonPredicate initWithLeftExpression rightExpression modifier type options example article.

NSComparisonPredicate customSelector example in Objective C (iOS).


NSComparisonPredicate customSelector

Returns the selector for the receiver.

- (SEL)customSelector

Return Value of [NSComparisonPredicate customSelector]
The selector for the receiver, or NULL if there is none.

NSComparisonPredicate customSelector example.
- (NSPredicate *)predicateWithEnhancedSubstitutionVariables:(NSDictionary *)vars {
  if ([self isKindOfClass:[NSCompoundPredicate class]]) {
    // recurse for compound predicates
    NSCompoundPredicate *compound = (NSCompoundPredicate *)self;
    NSMutableArray *subpredicates = [NSMutableArray array];
    for (NSPredicate *subpredicate in [compound subpredicates]) {
      NSPredicate *new = [subpredicate predicateWithEnhancedSubstitutionVariables:vars];
      [subpredicates addObject:new];
    }
    return [[[NSCompoundPredicate alloc] initWithType:[compound compoundPredicateType]
                                        subpredicates:subpredicates] autorelease];
  } else {
    NSPredicate *final = self;
    // on comparison predicates, pull out the left and right expressions
    NSComparisonPredicate *comparison = (NSComparisonPredicate *)self;
    NSExpression *left = [comparison leftExpression];

    // substitute, if appropriate
    if ([left expressionType] == NSVariableExpressionType) {
      NSString *variable = [left variable];
      if ([variable hasPrefix:@"KEY_"]) {
        NSString *replacement = [vars objectForKey:variable];
        if (replacement) {
          left = [NSExpression expressionForKeyPath:replacement];
        }
      }
    }

    // substitute, if appropriate
    NSExpression *right = [comparison rightExpression];
    if ([right expressionType] == NSVariableExpressionType) {
      NSString *variable = [right variable];
      if ([variable hasPrefix:@"KEY_"]) {
        NSString *replacement = [vars objectForKey:variable];
        if (replacement) {
          right = [NSExpression expressionForKeyPath:replacement];
        }
      }
    }

    // build and return the appropriate comparison predicate
    if (left != [comparison leftExpression] || right != [comparison rightExpression]) {
      if ([comparison customSelector] != NULL) {
        final = [NSComparisonPredicate predicateWithLeftExpression:left
                                                   rightExpression:right
                                                    customSelector:[comparison customSelector]];
      } else {
        final = [NSComparisonPredicate predicateWithLeftExpression:left
                                                   rightExpression:right
                                                          modifier:[comparison comparisonPredicateModifier]
                                                              type:[comparison predicateOperatorType]
                                                           options:[comparison options]];
      }
    }
    return [final predicateWithSubstitutionVariables:vars];
  }
}

End of NSComparisonPredicate customSelector example article.

NSComparisonPredicate comparisonPredicateModifier example in Objective C (iOS).


NSComparisonPredicate comparisonPredicateModifier

Returns the comparison predicate modifier for the receiver.

- (NSComparisonPredicateModifier)comparisonPredicateModifier

Return Value
The comparison predicate modifier for the receiver.

Discussion of [NSComparisonPredicate comparisonPredicateModifier]
The default value is NSDirectPredicateModifier.

NSComparisonPredicate comparisonPredicateModifier example.
- (NSPredicate *)predicateWithEnhancedSubstitutionVariables:(NSDictionary *)vars {
  if ([self isKindOfClass:[NSCompoundPredicate class]]) {
    // recurse for compound predicates
    NSCompoundPredicate *compound = (NSCompoundPredicate *)self;
    NSMutableArray *subpredicates = [NSMutableArray array];
    for (NSPredicate *subpredicate in [compound subpredicates]) {
      NSPredicate *new = [subpredicate predicateWithEnhancedSubstitutionVariables:vars];
      [subpredicates addObject:new];
    }
    return [[[NSCompoundPredicate alloc] initWithType:[compound compoundPredicateType]
                                        subpredicates:subpredicates] autorelease];
  } else {
    NSPredicate *final = self;
    // on comparison predicates, pull out the left and right expressions
    NSComparisonPredicate *comparison = (NSComparisonPredicate *)self;
    NSExpression *left = [comparison leftExpression];

    // substitute, if appropriate
    if ([left expressionType] == NSVariableExpressionType) {
      NSString *variable = [left variable];
      if ([variable hasPrefix:@"KEY_"]) {
        NSString *replacement = [vars objectForKey:variable];
        if (replacement) {
          left = [NSExpression expressionForKeyPath:replacement];
        }
      }
    }

    // substitute, if appropriate
    NSExpression *right = [comparison rightExpression];
    if ([right expressionType] == NSVariableExpressionType) {
      NSString *variable = [right variable];
      if ([variable hasPrefix:@"KEY_"]) {
        NSString *replacement = [vars objectForKey:variable];
        if (replacement) {
          right = [NSExpression expressionForKeyPath:replacement];
        }
      }
    }

    // build and return the appropriate comparison predicate
    if (left != [comparison leftExpression] || right != [comparison rightExpression]) {
      if ([comparison customSelector] != NULL) {
        final = [NSComparisonPredicate predicateWithLeftExpression:left
                                                   rightExpression:right
                                                    customSelector:[comparison customSelector]];
      } else {
        final = [NSComparisonPredicate predicateWithLeftExpression:left
                                                   rightExpression:right
                                                          modifier:[comparison comparisonPredicateModifier]
                                                              type:[comparison predicateOperatorType]
                                                           options:[comparison options]];
      }
    }
    return [final predicateWithSubstitutionVariables:vars];
  }
}

End of NSComparisonPredicate comparisonPredicateModifier example article.

NSComparisonPredicate predicateWithLeftExpression rightExpression modifier type options example in Objective C (iOS).


NSComparisonPredicate predicateWithLeftExpression rightExpression modifier type options

Creates and returns a predicate of a given type formed by combining given left and right expressions using a given modifier and options.

+ (NSPredicate *)predicateWithLeftExpression:(NSExpression *)lhs rightExpression:(NSExpression *)rhs modifier:(NSComparisonPredicateModifier)modifier type:(NSPredicateOperatorType)type options:(NSUInteger)options

Parameters of [NSComparisonPredicate predicateWithLeftExpression 
lhs
The left hand expression.
rhs
The right hand expression.
modifier
The modifier to apply.
type
The predicate operator type.
options
The options to apply (see “NSComparisonPredicate Options”). For no options, pass 0.

Return Value of [NSComparisonPredicate predicateWithLeftExpression rightExpression modifier type options]
A new predicate of type type formed by combining the given left and right expressions using the modifier and options.

NSComparisonPredicate predicateWithLeftExpression rightExpression modifier type options example.
-(NSPredicate *) predicateWithStudyID:(NSString *) aStudyID studyName:(NSString *) aStudyName date:(NSDate *) aDate{
  NSPredicate *p;
  NSMutableArray *preds=[[NSMutableArray alloc] initWithCapacity:1];
  if (aDate != nil) {
    NSExpression *dateKeyEx=[NSExpression expressionForKeyPath:@"studyDate"];
    NSExpression *aDateEx=[NSExpression expressionForConstantValue:aDate];
    NSPredicate *datePred=[NSComparisonPredicate predicateWithLeftExpression:dateKeyEx
                                                             rightExpression:aDateEx
                                                                    modifier:NSDirectPredicateModifier
                                                                        type:NSEqualToPredicateOperatorType
                                                                     options:0];
    [preds addObject:datePred];
  }
  if (![aStudyID isEqualToString:@""]) {
    NSExpression *studyIDKeyEx=[NSExpression expressionForKeyPath:@"studyID"];
    NSExpression *aStudyIDEx=[NSExpression expressionForConstantValue:aStudyID];
    NSPredicate *studyIDPred=[NSComparisonPredicate predicateWithLeftExpression:studyIDKeyEx
                                                                rightExpression:aStudyIDEx
                                                                       modifier:NSDirectPredicateModifier
                                                                           type:NSLikePredicateOperatorType
                                                                        options:NSCaseInsensitivePredicateOption];
    [preds addObject:studyIDPred];
  }
  if (![aStudyName isEqualToString:@""]) {
    NSExpression *studyNameKeyEx=[NSExpression expressionForKeyPath:@"studyName"];
    NSExpression *aStudyNameEx=[NSExpression expressionForConstantValue:aStudyName];
    NSPredicate *studyNamePred=[NSComparisonPredicate predicateWithLeftExpression:studyNameKeyEx
                                                                rightExpression:aStudyNameEx
                                                                       modifier:NSDirectPredicateModifier
                                                                           type:NSLikePredicateOperatorType
                                                                        options:NSCaseInsensitivePredicateOption];
    [preds addObject:studyNamePred];
  }
  p=[NSCompoundPredicate andPredicateWithSubpredicates:preds]; 
  [preds release];
  return p;
}

Example of [NSComparisonPredicate predicateWithLeftExpression rightExpression modifier type options].
// First part: name != <varName>
NSPredicate *p1 = [NSPredicate predicateWithFormat:@"name != %@", varName];

// Second part: date <op> <varDate>
NSExpression *lexp = [NSExpression expressionForKeyPath:@"date"];
NSExpression *rexp = [NSExpression expressionForConstantValue:varDate];
NSPredicateOperatorType op = (direction == kPageDirectionForward) ? NSGreaterThanPredicateOperatorType : NSLessThanOrEqualToPredicateOperatorType;
NSPredicate *p2 = [NSComparisonPredicate predicateWithLeftExpression:lexp
                                                     rightExpression:rexp
                                                            modifier:0
                                                                type:op
                                                             options:0];
// predicate = <p1> AND <p2>
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[p1, p2]];

NSComparisonPredicate predicateWithLeftExpression rightExpression modifier type options example.
NSPredicate *nameQuery =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_name"]
                                   rightExpression:[NSExpression expressionForConstantValue:Name]
                                          modifier:NSDirectPredicateModifier
                                              type:NSLikePredicateOperatorType
                                           options:NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption];

NSPredicate *regNoQuery =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_regno"]
                                   rightExpression:[NSExpression expressionForConstantValue:RegNo]
                                          modifier:NSDirectPredicateModifier
                                              type:NSEqualToPredicateOperatorType
                                           options:0];

NSPredicate *query = [NSCompoundPredicate andPredicateWithSubpredicates:@[nameQuery,regNoQuery]];

End of NSComparisonPredicate predicateWithLeftExpression rightExpression modifier type options example article.

NSComparisonPredicate predicateWithLeftExpression rightExpression customSelector example in Objective C (iOS).


NSComparisonPredicate predicateWithLeftExpression rightExpression customSelector

Returns a new predicate formed by combining the left and right expressions using a given selector.

+ (NSPredicate *)predicateWithLeftExpression:(NSExpression *)lhs rightExpression:(NSExpression *)rhs customSelector:(SEL)selector

Parameters of [NSComparisonPredicate predicateWithLeftExpression 
lhs
The left hand side expression.
rhs
The right hand side expression.
selector
The selector to use for comparison. The method defined by the selector must take a single argument and return a BOOL value.

Return Value of [NSComparisonPredicate predicateWithLeftExpression rightExpression customSelector]
A new predicate formed by combining the left and right expressions using selector.

NSComparisonPredicate predicateWithLeftExpression rightExpression customSelector example.
- (NSPredicate *)predicateWithEnhancedSubstitutionVariables:(NSDictionary *)vars {
  if ([self isKindOfClass:[NSCompoundPredicate class]]) {
    // recurse for compound predicates
    NSCompoundPredicate *compound = (NSCompoundPredicate *)self;
    NSMutableArray *subpredicates = [NSMutableArray array];
    for (NSPredicate *subpredicate in [compound subpredicates]) {
      NSPredicate *new = [subpredicate predicateWithEnhancedSubstitutionVariables:vars];
      [subpredicates addObject:new];
    }
    return [[[NSCompoundPredicate alloc] initWithType:[compound compoundPredicateType]
                                        subpredicates:subpredicates] autorelease];
  } else {
    NSPredicate *final = self;
    // on comparison predicates, pull out the left and right expressions
    NSComparisonPredicate *comparison = (NSComparisonPredicate *)self;
    NSExpression *left = [comparison leftExpression];

    // substitute, if appropriate
    if ([left expressionType] == NSVariableExpressionType) {
      NSString *variable = [left variable];
      if ([variable hasPrefix:@"KEY_"]) {
        NSString *replacement = [vars objectForKey:variable];
        if (replacement) {
          left = [NSExpression expressionForKeyPath:replacement];
        }
      }
    }

    // substitute, if appropriate
    NSExpression *right = [comparison rightExpression];
    if ([right expressionType] == NSVariableExpressionType) {
      NSString *variable = [right variable];
      if ([variable hasPrefix:@"KEY_"]) {
        NSString *replacement = [vars objectForKey:variable];
        if (replacement) {
          right = [NSExpression expressionForKeyPath:replacement];
        }
      }
    }

    // build and return the appropriate comparison predicate
    if (left != [comparison leftExpression] || right != [comparison rightExpression]) {
      if ([comparison customSelector] != NULL) {
        final = [NSComparisonPredicate predicateWithLeftExpression:left
                                                   rightExpression:right
                                                    customSelector:[comparison customSelector]];
      } else {
        final = [NSComparisonPredicate predicateWithLeftExpression:left
                                                   rightExpression:right
                                                          modifier:[comparison comparisonPredicateModifier]
                                                              type:[comparison predicateOperatorType]
                                                           options:[comparison options]];
      }
    }
    return [final predicateWithSubstitutionVariables:vars];
  }
}

Example of [NSComparisonPredicate predicateWithLeftExpression rightExpression customSelector].
NSExpression * leftExpression = [NSExpression expressionForKeyPath:@"name"];
NSExpression * rightExpression = [NSExpression expressionForConstantValue:@"Hello!"];

NSComparisonPredicate * predicate = [NSComparisonPredicate predicateWithLeftExpression:leftExpression
                                                                       rightExpression:rightExpression
                                                                              modifier:NSDirectPredicateModifier
                                                                                  type:NSContainsPredicateOperatorType
                                                                               options:(NSCaseInsensitivePredicateOption | NSDiacriticInsensitivePredicateOption)];

NSComparisonPredicate predicateWithLeftExpression rightExpression customSelector example.
NSPredicate *nameQuery =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_name"]
                                   rightExpression:[NSExpression expressionForConstantValue:Name]
                                          modifier:NSDirectPredicateModifier
                                              type:NSLikePredicateOperatorType
                                           options:NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption];

NSPredicate *regNoQuery =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_regno"]
                                   rightExpression:[NSExpression expressionForConstantValue:RegNo]
                                          modifier:NSDirectPredicateModifier
                                              type:NSEqualToPredicateOperatorType
                                           options:0];

NSPredicate *query = [NSCompoundPredicate andPredicateWithSubpredicates:@[nameQuery,regNoQuery]];

End of NSComparisonPredicate predicateWithLeftExpression rightExpression customSelector example article.

NSComparisonPredicate predicateWithLeftExpression example in Objective C (iOS).


NSComparisonPredicate predicateWithLeftExpression

Returns a new predicate formed by combining the left and right expressions using a given selector.

+ (NSPredicate *)predicateWithLeftExpression:(NSExpression *)lhs rightExpression:(NSExpression *)rhs customSelector:(SEL)selector

Parameters
lhs
The left hand side expression.
rhs
The right hand side expression.
selector
The selector to use for comparison. The method defined by the selector must take a single argument and return a BOOL value.

Return Value of [NSComparisonPredicate predicateWithLeftExpression]
A new predicate formed by combining the left and right expressions using selector.

NSComparisonPredicate predicateWithLeftExpression example.
- (NSPredicate *)predicateWithEnhancedSubstitutionVariables:(NSDictionary *)vars {
  if ([self isKindOfClass:[NSCompoundPredicate class]]) {
    // recurse for compound predicates
    NSCompoundPredicate *compound = (NSCompoundPredicate *)self;
    NSMutableArray *subpredicates = [NSMutableArray array];
    for (NSPredicate *subpredicate in [compound subpredicates]) {
      NSPredicate *new = [subpredicate predicateWithEnhancedSubstitutionVariables:vars];
      [subpredicates addObject:new];
    }
    return [[[NSCompoundPredicate alloc] initWithType:[compound compoundPredicateType]
                                        subpredicates:subpredicates] autorelease];
  } else {
    NSPredicate *final = self;
    // on comparison predicates, pull out the left and right expressions
    NSComparisonPredicate *comparison = (NSComparisonPredicate *)self;
    NSExpression *left = [comparison leftExpression];

    // substitute, if appropriate
    if ([left expressionType] == NSVariableExpressionType) {
      NSString *variable = [left variable];
      if ([variable hasPrefix:@"KEY_"]) {
        NSString *replacement = [vars objectForKey:variable];
        if (replacement) {
          left = [NSExpression expressionForKeyPath:replacement];
        }
      }
    }

    // substitute, if appropriate
    NSExpression *right = [comparison rightExpression];
    if ([right expressionType] == NSVariableExpressionType) {
      NSString *variable = [right variable];
      if ([variable hasPrefix:@"KEY_"]) {
        NSString *replacement = [vars objectForKey:variable];
        if (replacement) {
          right = [NSExpression expressionForKeyPath:replacement];
        }
      }
    }

    // build and return the appropriate comparison predicate
    if (left != [comparison leftExpression] || right != [comparison rightExpression]) {
      if ([comparison customSelector] != NULL) {
        final = [NSComparisonPredicate predicateWithLeftExpression:left
                                                   rightExpression:right
                                                    customSelector:[comparison customSelector]];
      } else {
        final = [NSComparisonPredicate predicateWithLeftExpression:left
                                                   rightExpression:right
                                                          modifier:[comparison comparisonPredicateModifier]
                                                              type:[comparison predicateOperatorType]
                                                           options:[comparison options]];
      }
    }
    return [final predicateWithSubstitutionVariables:vars];
  }
}

Example of [NSComparisonPredicate predicateWithLeftExpression].
NSExpression * leftExpression = [NSExpression expressionForKeyPath:@"name"];
NSExpression * rightExpression = [NSExpression expressionForConstantValue:@"Hello!"];

NSComparisonPredicate * predicate = [NSComparisonPredicate predicateWithLeftExpression:leftExpression
                                                                       rightExpression:rightExpression
                                                                              modifier:NSDirectPredicateModifier
                                                                                  type:NSContainsPredicateOperatorType
                                                                               options:(NSCaseInsensitivePredicateOption | NSDiacriticInsensitivePredicateOption)];

NSComparisonPredicate predicateWithLeftExpression example.
NSPredicate *nameQuery =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_name"]
                                   rightExpression:[NSExpression expressionForConstantValue:Name]
                                          modifier:NSDirectPredicateModifier
                                              type:NSLikePredicateOperatorType
                                           options:NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption];

NSPredicate *regNoQuery =
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"s_regno"]
                                   rightExpression:[NSExpression expressionForConstantValue:RegNo]
                                          modifier:NSDirectPredicateModifier
                                              type:NSEqualToPredicateOperatorType
                                           options:0];

NSPredicate *query = [NSCompoundPredicate andPredicateWithSubpredicates:@[nameQuery,regNoQuery]];

End of NSComparisonPredicate predicateWithLeftExpression example article.