Friday, June 14, 2013

NSComparisonPredicate initWithLeftExpression example in Objective C (iOS).


NSComparisonPredicate initWithLeftExpression

Initializes a predicate formed by combining given left and right expressions using a given selector.

- (id)initWithLeftExpression:(NSExpression *)lhs rightExpression:(NSExpression *)rhs customSelector:(SEL)selector

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

Return Value of [NSComparisonPredicate initWithLeftExpression]
The receiver, initialized by combining the left and right expressions using selector.

NSComparisonPredicate initWithLeftExpression 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 example article.