NSPredicateEditor& NSExpression-显示可以与谓词的值不同吗? [英] NSPredicateEditor & NSExpression - Can the display be different than the value for the predicate?
问题描述
我有一个谓词编辑器,该模板是通过以下方式生成的:
I have a predicate editor, which the template was generate via the following:
NSArray * test = [NSArray arrayWithObjects:
[NSExpression expressionForKeyPath: @"Abc"],
[NSExpression expressionForKeyPath: @"Def"],
nil];
NSPredicateEditorRowTemplate * template = [[NSPredicateEditorRowTemplate alloc] initWithLeftExpressions: test
rightExpressionAttributeType: NSStringAttributeType
modifier: NSDirectPredicateModifier
operators: [NSArray arrayWithObject:
[NSNumber numberWithUnsignedInteger:NSContainsPredicateOperatorType]]
options:(NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption)];
因此,如果我填写这样的谓词编辑器:
So if I fill in a predicate editor like this:
注销生成的谓词时,我得到:
When I log out the generated predicate I get:
Abc CONTAINS[cd] "abc" OR Def CONTAINS[cd] "def"
我想知道的是,我能否以某种方式使谓词编辑器模板的显示与在生成的谓词中设置的值不同.
What I'm wondering is if I can somehow have the predicate editors template display be different than the value that gets set in the generated predicate.
EX:我希望输出谓词具有:
EX: I want the output predicate to have:
Field1 CONTAINS[cd] "abc" OR Field2 CONTAINS[cd] "def"
即使编辑器仍将abc
和def
作为字段显示.这可能吗?
Even though the editor still displays abc
and def
as the fields. Is this possible?
推荐答案
是的,您可以这样做.
您希望左表达式的数组成为最终谓词中的实际keyPath.就您而言,"Field1"
和"Field2"
.
You want the array of left expressions to be the actual keyPaths in the final predicate. In your case, "Field1"
and "Field2"
.
关于使不同的值出现在弹出窗口中,这是一种令人着迷的概念:
As for making a different value appear in the popup, here's where a mind-bending concept comes in:
您将要把谓词编辑器本地化为英语.
您可以通过两种方式进行此操作.
There are two ways you could do this.
- 带有.strings文件
- 使用
NSDictionary
带有.strings文件
在您的来源中,您需要在评论中添加以下内容:
In your source, you would include the following in a comment:
// NSLocalizedStringFromTable(@"%[Field1,Field2]@ %[contains]@ %@", @"PredicateEditor", @"")
运行 genstrings
在您的源代码上,这将生成一个PredicateEditor.strings
文件,其中包含以下条目:
When you run genstrings
on your source code, this will generate a PredicateEditor.strings
file with the following entries:
"%[Field1]@ %[contains]@ %@" = "%[Field1]@ %[contains]@ %@";
"%[Field2]@ %[contains]@ %@" = "%[Field2]@ %[contains]@ %@";
您将值更改为:
"%[Field1]@ %[contains]@ %@" = "%[Abc]@ %[contains]@ %@";
"%[Field2]@ %[contains]@ %@" = "%[Def]@ %[contains]@ %@";
然后,当您创建NSPredicateEditor
时,将设置属性设置为//apple_ref/doc/uid/TP40004576-CH1-SW4> formattingStringsFileName
属性,其余部分将由编辑器负责.
Then, when you create your NSPredicateEditor
, you would set the formattingStringsFileName
property to "PredicateEditor"
, and the editor will take care of the rest.
使用NSDictionary
With an NSDictionary
这将遵循与.strings选项相同的基本概念,不同之处在于您实际上会这样做:
This would follow the same fundamental concepts as the .strings option, except that you would essentially do:
NSDictionary *formatting = @{
@"%[Field1]@ %[contains]@ %@" : @"%[Abc]@ %[contains]@ %@",
@"%[Field2]@ %[contains]@ %@" : @"%[Def]@ %[contains]@ %@"
}
[myPredicateEditor setFormattingDictionary:formatting];
这就是您要做的一切.
我很久以前就写过博客,其中包含的更多信息可能对您有用
I blogged about this a long time ago, and that has more information that you might find useful.
这篇关于NSPredicateEditor& NSExpression-显示可以与谓词的值不同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!