使用NSPredicateEditor强制显示复合行

ndg*_*ndg 5 xcode cocoa nspredicateeditor nspredicate

我正在代码中创建一个NSPredicateEditor,并试图实现我希望的相当简单的任务.基本上,我想显示一个复合NSPredicateEditorRowTemplate(给用户提供在"All/Any/None以下都是真"时执行匹配的选项)和它下面的一些相当基本的子行.为此,我构造我的NSPredicateEditor并绑定其值,以便在用户编辑其谓词时保存更改.

我遇到的问题是,无论如何,我都不能将复合NSPredicateEditorRowTemplate默认显示为具有单个子行的父行.当我最初创建谓词时,我传递了一个虚假的空谓词,如下所示:

filename BEGINSWITH[cd] ''
Run Code Online (Sandbox Code Playgroud)

为了强制显示复合行,我必须创建两个子行:

filename BEGINSWITH[cd] '' && path ==[cd] ''
Run Code Online (Sandbox Code Playgroud)

作为参考,这是我如何构建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];
Run Code Online (Sandbox Code Playgroud)

Dav*_*ong 5

问题是你给它一个比较谓词.你需要给它一个复合的.

我猜你的SIPredicateStringValueTransformer对象正在使用一个字符串并将其转换为谓词,对吧?它可能会做这样的事情:

- (id) transformedValue:(id)value {
  return [NSPredicate predicateWithFormat:value];
}
Run Code Online (Sandbox Code Playgroud)

你想要做的是保证值变换器不仅返回任何旧的谓词,而是返回复合谓词(即一个NSCompoundPredicate而不是一个NSComparisonPredicate).这样做的方法很简单:

- (id) transformedValue:(id)value {
  NSPredicate *p = [NSPredicate predicateWithFormat:value];
  if ([p isKindOfClass:[NSComparisonPredicate class]]) {
    p = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObject:p]];
  }
  return p;
}
Run Code Online (Sandbox Code Playgroud)

当您给NSCompoundPredicate谓词编辑器时,它会显示复合行.当您仅提供比较谓词时,它仅显示相应的比较行.