NSCompoundPredicate 匹配失败
我正在使用下面的代码为 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];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的谓词要求可过滤对象中的所有值都是字符串。这是正确的吗?
另外,我会将您的子谓词创建简化为:
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: