NSCompoundPredicate 匹配失败

发布于 2024-09-13 04:37:36 字数 1050 浏览 8 评论 0原文

我正在使用下面的代码为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见终念 2024-09-20 04:37:36

您的谓词要求可过滤对象中的所有值都是字符串。这是正确的吗?

另外,我会将您的子谓词创建简化为:

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

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]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文