views:

58

answers:

1

I'm building a NSPredicate using the code below for an iPhone app. The logging shows the prediate to be: location CONTAINS "head" AND shape CONTAINS "oval" AND texture CONTAINS "bumpy" AND colour CONTAINS "red"

I get no results. If I limit the predicate to a single item it will work, more than 1 fails.

Can anyone tell me why?

Many thanks

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];
A: 

Your predicate is requiring that all of the values in your filterable objects be strings. Is that correct?

Also, I would simplify your subpredicate creation to:

NSPredicate * p = [NSPredicate predicateWithFormat:@"%K CONTAINS %@", [Title lowercaseString], [Value lowercaseString]];
Dave DeLong
Thanks for replying, yes they will all be string values.
BigBadOwl
Actually it works just fine, I'm freakin' idiot!
BigBadOwl