Итак, я не знаю встроенного способа сделать это. Тем не менее, это довольно просто написать самому. Вы можете легко написать метод категории NSPredicate
, который заменяет переменные, начинающиеся с KEY_
, на выражение пути ключа, например:
- (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];
}
}
С этим вы должны уметь:
NSPredicate *p = [NSPredicate predicateWithFormat:@"$KEY_DATE = $DATE"];
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:@"myDateProperty", @"KEY_DATE", aDate, @"date", nil];
p = [p predicateWithEnhancedSubstitutionVariables:d];
И верните предикат, как если бы вы сделали:
NSPredicate *p = [NSPredicate predicateWithFormat:@"myDateProperty = %@", aDate];
предупреждение: набрано в браузере, не скомпилировано, yadda yadda