Custom Typedef

Discussion in 'iOS Programming' started by ahan.tm, Apr 27, 2013.

  1. ahan.tm, Apr 27, 2013
    Last edited: Apr 27, 2013

    ahan.tm macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #1
    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!
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Symbols can not start with a numeral.

    They must start with any of a-z, A-Z or an underscore. Follow on character may include 0-9.
     
  3. ahan.tm thread starter macrumors regular

    Joined:
    Jun 26, 2011
    Location:
    Florida
    #3
    Hmm. I think that a class might be a better idea.

    Thanks!
     
  4. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #4
    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.
     

Share This Page