2012-01-03 13 views
2

動的NSPredicatesを構築する必要があります。私は置換変数を適用できる文字列ファイルを使用するのが非常に便利だとわかりました。これが私がファイルで行ったことです。NSPredicateテンプレートの代替フィールド名

start<=$START AND end>=$END 

、これはユーティリティメソッド内に存在するコード断片である:それはすべてが非常にうまく機能

-(NSPredicate*)predicateWithName:(NSString*)name andVariables:(NSDictionary*)dict { 
    NSURL *queryFile = [[NSBundle mainBundle] URLForResource:name withExtension:@"query"]; 
    //... 
    NSString *query = [NSString stringWithContentsOfURL:queryFile encoding:NSStringEncodingConversionAllowLossy error:&err]; 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:query]; 
    return [predicate predicateWithSubstitutionVariables:dict]; 
} 

が、今、私はいくつかの条件Iに基づいて、動的にフィールドを追加することが必要で午前特定のフィールドを照会したいのですが、これを達成する方法を見つけることができません。クエリファイルは次のようになります。

start<=$START AND end>=$END AND $FIELD_NAME==1 

$ FIELD_NAME名は引用符で置き換えられ、クエリは機能しません。私はキーに%Kを使うことができますが、辞書でこれを行う方法はないようです。

ありがとうございました

答えて

2

私はこれを行うための方法がありません。しかし、あなた自身を書くのはとても簡単です。これにより

- (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]; 
    } 
} 

、あなたが行うことができるはず:あなたは簡単にこのように、キーパス式でKEY_で始まる変数に置き換えられNSPredicateカテゴリメソッドを書くことができ

NSPredicate *p = [NSPredicate predicateWithFormat:@"$KEY_DATE = $DATE"]; 
NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:@"myDateProperty", @"KEY_DATE", aDate, @"date", nil]; 

p = [p predicateWithEnhancedSubstitutionVariables:d]; 

とGETバック述語あなたが行っていたかのように:

NSPredicate *p = [NSPredicate predicateWithFormat:@"myDateProperty = %@", aDate]; 

警告:ブラウザで入力し、コンパイルされていない、yadda yaddaを