Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ahan.tm

macrumors regular
Original poster
Jun 26, 2011
141
0
Florida
Hi,

I am creating an application for scoring regattas. I was trying to create a custom typedef that would be 1A, 1B, 2A, 2B, 3A, 3B, etc, with the maximum number definable at runtime(ie. go up to 10B). Any ideas on how to create a typedef like this with an int and an NSString?

Thanks!
 
Last edited:
Hi,

I am creating an application for scoring regattas. I was trying to create a custom typedef that would be 1A, 1B, 2A, 2B, 3A, 3B, etc, with the maximum number definable at runtime(ie. go up to 10B). Any ideas on how to create a typedef like this with an int and an NSString?

Thanks!

You can certainly create a typdef'ed enum, but enums are integers, and the symbols for the enum values will only be meaningful in your code, not displayed to your user.

As the other poster said, I don't think you can begin the name of an enum with a digit. You need to start it with a letter.

The syntax for that is as follows:


Code:
typedef enum
{
  score1A,
  score1B, 
  score2A,
  score2B
} scoreValues;

That enum will create symbols that you can use in your code, like this:

Code:
scoreValues currentScore;

currentScore = score1A;

Those enums will have numeric values starting with 0.

You can then create an array of display strings:

Code:
NSArray *scoreNames = @[@"1A", @"1B", @"2A", @"2B"];

And you can use a variable of type scoreValues as an index into your array of score names:

Code:
NSString *scoreName  = scoreNames[currentScore];

I don't think you need to cast the enum to an integer, but if you get a compiler error and do need to cast it, that would look like this:


Code:
NSString *scoreName  = scoreNames[(NSUInteger)currentScore];

If you want the maximum score index to be settable at runtime you should probably just add a variable maxScore and write your code to use that limit.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.