PredicateOptimiser is all about doing cool things with NSPredicate objects. There are two classes:
POPredicateOptimiserThis class can be used to optimise yourNSPredicateobject.POPredicateFormatterThis class can be used to format yourNSPredicateobject.
POPredicateOptimiser has a single method optimisedPredicateForPredicate:. Passing it an NSPredicate object will return you a new (possibly optmised) NSPredicate object in accordance property optimisationType.
optimisationType takes one of the following values:
POPredicateOptimisationTypeNoneThe optimiser will simply return a copy of theNSPredicateobject.POPredicateOptimisationTypeBestThe optimiser will do its best to optimise theNSPredicateobject.
For example:
| Before | After |
|---|---|
not (a > b) |
a <= b |
... OR TRUEPREDICATE OR ... |
TRUEPREDICATE |
... AND FALSEPREDICATE AND ... |
FALSEPREDICATE |
POPredicateFormatter has a single method stringFromPredicate:. Passing it an NSPredicate object will return you an NSString in accordance with the property displayType.
displayType takes one of the following values:
POPredicateDisplayTypeStringThe formatter will simply return the result of callingdescriptionon theNSPredicateobject.POPredicateDisplayTypeCocoaThe formatter will return anNSStringcontaining the code neccessary to reproduce theNSPredicateobject in code.
For example, string contains[cd] 'Foo' would produce:
[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionWithFormat:@"string" /* NSKeyPathExpression */rightExpression:[NSExpression expressionWithFormat:@""Foo"" /* NSConstantValueExpression */ modifier:NSDirectPredicateModifier type:NSContainsPredicateOperatorType options:NSCaseInsensitivePredicateOption | NSDiacriticInsensitivePredicateOption]The license for the code is included with this project; it's basically a BSD license with attribution.