Friday, June 14, 2013

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.