Iphone -- maintaining a list of strings and a corresponding typedef enum

Posted by William Jockusch on Stack Overflow See other posts from Stack Overflow or by William Jockusch
Published on 2010-03-28T18:47:10Z Indexed on 2010/03/28 18:53 UTC
Read the original article Hit count: 187

Filed under:
|

Suppose I have the following:

typedef enum functionType {ln, sin, sqrt} functionType;
NSArray *functions = [NSArray arrayWithObjects: @"ln", @"sin", @"sqrt", nil];

Suppose further that *functions will not change at runtime.

Question -- is there any way to set up a single structure which updates both of these? So that I only have to keep track of one list, instead of two.

To explain what is going on -- the idea is that string input from the user will be stored in a variable of type functionType. Later on, I will have code like this:

double valueOfFunction: (functionType) function withInput: (double) input
  switch (function) {
    case ln:
      return ln(input);
    case sin:
      return sin(input);
    case sqrt:
      return sqrt(input);
    //etc . . . could grow to include a lot of functions.
  }

And valueOfFunction needs to be fast. So I don't want to be doing string comparisons there.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about enums