iphone - NSCompoundPredicate 匹配失败

标签 iphone cocoa sqlite nspredicate

我正在使用以下代码为 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/

相关文章:

cocoa - Xcode 3.2 中缺少 "find options"

objective-c - NSTextView 的意外行为(因为没有标题栏的 NSWindow?)

c++ - 使用 Sqlite3 blob 的 C++ 程序中的 RAM 消耗

iphone - 连接失败后调用 viewDidLoad 无效

iphone - iPhone 上应用程序的内存占用

html - Apple iphone 导致按钮在网站上被压扁

swift - 在 Swift 中为核心数据实现 NSValueTransformer 的位置

iPhone操作系统4.0 : Application is not coming applicationDidBecomeActive

带有 JSON1 的 Ubuntu 16.04 上的 SQLite

c# - OS X 上的 Mono : System. Data.SQLite 不工作