Friday, June 14, 2013

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.