Reputation: 7895
I am looking to create a basic expression language that I can leverage within an iOS application. I will be pulling these values in from a file at runtime. In the past I have just leveraged regular expressions for this, but it has its own limitations (as I basically just have two operands and an operator). I want to have a bit more robust support for expressions like the following:
Some examples:
valueA != valueB
(valueA == valueB) && (valueC != valueD)
valueA >= valueB
Basically, I would want to provide a dictionary to this expression evaluator and have it pull the values for the variables from that dictionary.
Any thoughts on an efficient way to get this done? I've done about 5 minutes of research this morning on CoreParse and ParseKit, but I am new to both (and parser generators as a whole).
Upvotes: 2
Views: 223
Reputation: 539805
You could use NSPredicate
, for example:
NSString *expr = @"($valueA == $valueB) && ($valueC != $valueD)";
NSPredicate *predicate = [NSPredicate predicateWithFormat:expr];
NSDictionary *vars = @{@"valueA": @7, @"valueB" : @7, @"valueC": @3, @"valueD": @4};
BOOL result = [predicate evaluateWithObject:nil substitutionVariables:vars];
Note that all variables have to start with a $
in the predicate string.
Upvotes: 4