我正在代码中创建一个NSPredicateEditor,并尝试实现我希望的相当简单的任务。基本上,我想显示一个复合NSPredicateEditorRowTemplate(给用户提供在“All / Any / None以下都是真”时执行匹配的选项)和它下面的一些相当基本的子行。为此,我构造我的NSPredicateEditor并绑定其值,以便在用户编辑其谓词时保存更改。
我遇到的问题是,无论如何,我都无法将复合NSPredicateEditorRowTemplate默认显示为具有单个子行的父行。当我最初创建谓词时,我传递了一个虚假的空谓词,如下所示:
filename BEGINSWITH[cd] ''
为了强制显示复合行,我必须创建两个子行:
filename BEGINSWITH[cd] '' && path ==[cd] ''
供参考,以下是我如何构建NSPredicateEditor:
NSArray *keyPaths = [NSArray arrayWithObjects:[NSExpression expressionForKeyPath:@"filename"], [NSExpression expressionForKeyPath:@"path"], nil];
NSArray *operators = [NSArray arrayWithObjects:[NSNumber numberWithInteger:NSEqualToPredicateOperatorType],
[NSNumber numberWithInteger:NSNotEqualToPredicateOperatorType],
[NSNumber numberWithInteger:NSBeginsWithPredicateOperatorType],
[NSNumber numberWithInteger:NSEndsWithPredicateOperatorType],
[NSNumber numberWithInteger:NSContainsPredicateOperatorType],
nil];
NSPredicateEditorRowTemplate *template = [[NSPredicateEditorRowTemplate alloc] initWithLeftExpressions:keyPaths
rightExpressionAttributeType:NSStringAttributeType
modifier:NSDirectPredicateModifier
operators:operators
options:(NSCaseInsensitivePredicateOption | NSDiacriticInsensitivePredicateOption)];
NSArray *compoundTypes = [NSArray arrayWithObjects:[NSNumber numberWithInteger:NSNotPredicateType],
[NSNumber numberWithInteger:NSAndPredicateType],
[NSNumber numberWithInteger:NSOrPredicateType],
nil];
NSPredicateEditorRowTemplate *compound = [[NSPredicateEditorRowTemplate alloc] initWithCompoundTypes:compoundTypes];
NSArray *rowTemplates = [NSArray arrayWithObjects:compound, template, nil];
[template release];
[compound release];
predicateEditor = [[NSPredicateEditor alloc] init];
[predicateEditor setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[predicateEditor setRowTemplates:rowTemplates];
[predicateEditor setCanRemoveAllRows:NO];
[predicateEditor setContinuous:YES];
[predicateEditor setFrame:[[scrollView contentView] bounds]];
// Create a custom binding for our NSPredicateEditor
SIPredicateStringValueTransformer *transformer = [[[SIPredicateStringValueTransformer alloc] init] autorelease];
NSMutableDictionary *bindingOptions = [NSMutableDictionary dictionaryWithObjectsAndKeys:transformer, NSValueTransformerBindingOption, [NSNumber numberWithBool:YES], NSValidatesImmediatelyBindingOption, nil];
[predicateEditor bind:@"value" toObject:self withKeyPath:@"self.filter.predicate" options:bindingOptions];
// Add our NSPredicateEditor to our NSScrollView
[scrollView setDocumentView:predicateEditor];
答案 0 :(得分:5)
问题在于你给它一个比较谓词。你需要给它一个复合物。
我猜你的SIPredicateStringValueTransformer
对象正在使用字符串并将其转换为谓词,对吧?它可能正在做这样的事情:
- (id) transformedValue:(id)value {
return [NSPredicate predicateWithFormat:value];
}
您想要做的是保证值变换器不仅返回任何旧谓词,而是返回复合谓词(即NSCompoundPredicate
而不是{{1 }})。这样做的方法很简单:
NSComparisonPredicate
当您为谓词编辑器提供- (id) transformedValue:(id)value {
NSPredicate *p = [NSPredicate predicateWithFormat:value];
if ([p isKindOfClass:[NSComparisonPredicate class]]) {
p = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObject:p]];
}
return p;
}
时,它会显示复合行。当您只提供比较谓词时,它只显示相应的比较行。