Friday, June 14, 2013

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.