2010-08-09 23 views
1

我正在使用下面的代碼爲iPhone應用程序構建NSPredicate。日誌顯示prediate是:位置CONTAINS「頭」和形狀包含「橢圓形」和紋理包含「顛簸」和顏色包含「紅色」NSCompoundPredicate未能匹配

我沒有得到任何結果。如果我將謂詞限制爲單個項目,它將起作用,超過1個失敗。

有誰能告訴我爲什麼?

非常感謝

NSMutableArray *subPredicates = [[NSMutableArray alloc] init]; 
for (Ditem in self.tableDataSource) { 
    NSString *Title = [Ditem valueForKey:@"Title"]; 
    NSString *Value = [Ditem valueForKey:@"Value"]; 
    if([[Value lowercaseString] isEqualToString: @"all"]){ 
     Value = @""; 
    } 
    else{ 
     NSPredicate *p = [NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:[Title lowercaseString]] rightExpression:[NSExpression expressionForConstantValue:[Value lowercaseString]] modifier:NSDirectPredicateModifier type:NSContainsPredicateOperatorType options:0]; 
     [subPredicates addObject:p]; 
    } 
} 
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subPredicates]; 
NSLog(@"predicate: %@", predicate);[self.fetchedResultsController.fetchRequest setPredicate:predicate]; 
+0

要添加一些更多的信息。我所追求的將與SELECT * FROM表WHERE(位置LIKE'%head%')和(形狀LIKE'%oval%')和(紋理LIKE'%bumpy%')AND(顏色LIKE'%紅色%') – BigBadOwl 2010-08-09 18:02:30

回答

0

你的謂詞要求所有的值在過濾的對象是字符串。那是對的嗎?

另外,我想簡化subpredicate創建於:

NSPredicate * p = [NSPredicate predicateWithFormat:@"%K CONTAINS %@", [Title lowercaseString], [Value lowercaseString]]; 
+0

感謝您的回覆,是的,他們都將是字符串值。 – BigBadOwl 2010-08-09 17:59:26

+0

其實它工作得很好,我很瘋狂'白癡! – BigBadOwl 2010-08-11 15:52:07