2014-02-15 26 views
0

NSPredicate更換變量在NSPredicate

[NSPredicate predicateWithFormat:@"[email protected] == 50"] ; 

我怎樣才能得到一個新的NSPredicate從這一個,通過更換通過self(或沒有)$1?我怎樣才能得到謂詞

[NSPredicate predicateWithFormat:@"[email protected] == 50"] ; 

我不想評估我的謂詞,我想有一個新的謂詞。

回答

1

您可以從「謂詞模板」創建謂詞。然而,$1不在謂詞模板有效 變量和

[NSPredicate predicateWithFormat:@"[email protected] == 50"] 

實際上導致運行時異常。但是,下面的作品,並應表現出的理念:

NSPredicate *p1 = [NSPredicate predicateWithFormat:@"[email protected] == 50"] ; 
NSLog(@"%@", p1); 
// [email protected] == 50 

// Substitute $foo by self: 
NSPredicate *p2 = [p1 predicateWithSubstitutionVariables:@{@"foo": [NSExpression expressionForEvaluatedObject]}]; 
NSLog(@"%@", p2); 
// [email protected] == 50 

// Substitute $foo by "bar": 
NSPredicate *p3 = [p1 predicateWithSubstitutionVariables:@{@"foo": [NSExpression expressionForKeyPath:@"bar"]}]; 
NSLog(@"%@", p3); 
// [email protected] == 50     

欲瞭解更多信息,請參見「謂語編程指南」中"Creating Predicates Using Predicate Templates"

+0

我愛你@馬丁= 8-°<3 _/\\ _ – Colas

+0

@Colas:接受答案怎麼樣? –

+0

是的你是對的,我在等着看有沒有人會帶更好的東西......但是怎麼可能呢? – Colas