The usual OO way to do this is to create a class "mathOperator" that has a method "performOp" taking two parameters, then inherit different classes from it that represent the different operations. I'm not an expert in objective-C, so my syntax is probably a bit off, but I think you'd write something like:
result = [var On:arg1 And:arg2];
for example
result = [var On:2 And:3];
would set result
to 5 or 6 depending on whether var
was set to add
or mul
. The implementation would look like (again, very roughly):
@interface Add: mathOperator
-(int)On: (int)arg1 And: (int)arg2;
@end
...
@implementation Add
-(int)On: (int)arg1 And: (int)arg2 {
return arg1 + arg2;
}
and of course similar for the other operators:
@implementation Mul
-(int)On: (int)arg1 And: (int)arg2 {
return arg1 * arg2;
}