我正在使用以下代码为 iPhone 应用程序构建 NSPredicate。日志记录显示prediate为:位置包含“头”并且形状包含“椭圆形”并且纹理包含“凹凸不平”并且颜色包含“红色”
我没有得到任何结果。如果我将谓词限制为单个项目,它将起作用,超过 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];
最佳答案
您的谓词要求可过滤对象中的所有值都是字符串。这是正确的吗?
此外,我会将您的子谓词创建简化为:
NSPredicate * p = [NSPredicate predicateWithFormat:@"%K CONTAINS %@", [Title lowercaseString], [Value lowercaseString]];
关于iphone - NSCompoundPredicate 匹配失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3442104/