Friday, June 14, 2013

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.