Skip to content
davedelong edited this page Sep 27, 2011 · 1 revision

If you don't know what the value of a particular term should be when the string is constructed, that's ok; simply use a variable:

NSString *math = @"6 * $a";

When you figure out what the value is supposed to be, you can pass it along in the substitution dictionary:

NSDictionary *variableSubstitutions = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:7] forKey:@"a"];
NSLog(@"%@", [[DDMathEvaluator sharedMathEvaluator] evaluateString:math withSubstitutions:variableSubstitutions]); //logs "42"

Variables are denoted in the source string as beginning with $ and can contain numbers or letters. They are case sensitive. ($a is not the same as $A)