NSComparisonPredicate comparisonPredicateModifier
- (NSComparisonPredicateModifier)comparisonPredicateModifier
Return Value
The comparison predicate modifier for the receiver.
Discussion of [NSComparisonPredicate comparisonPredicateModifier]
The default value is NSDirectPredicateModifier.
NSComparisonPredicate comparisonPredicateModifier 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];
}
}
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];
}
}