C, obj c enum without tag or id

im learning cocos2d [open gl wrapper for objective C on iPhone] and now playing with sprites found this in the example,

 enum {  
easySprite =   0x0000000a,
mediumSprite = 0x0000000b,
hardSprite =   0x0000000c,
backButton =   0x0000000d,
magneticSprite = 0x0000000e,
magneticSprite2 = 0x0000000f
};

      

...

 -(id) init
 {...
  /second sprite
    TSprite *med = [TSprite spriteWithFile:@"butonB.png"]; //blue
    [med SetCanTrack:YES];
    [self addChild: med z:1 tag:mediumSprite];
    med.position=ccp(299,230);
    [TSprite track:med];

      

so the variable defined in the enum is used in the tag name of the generated sprite object,

but I do not understand

  • why give values ​​in hexa for tags to use
  • enum with dropdown tags

how i knew this enum in obj C and C

     typedef enum {
     JPG,
     PNG,
      GIF,
     PVR
     } kImageType;

      

thank!

-1


source to share


3 answers


Enumerations are automatically assigned to values, incremented from 0, but you can assign your own values.

If you do not provide any values, they will start at 0, as in:

typedef enum {
 JPG,
 PNG,
 GIF,
 PVR
 } kImageType;

      

But you can assign values ​​to them:

typedef enum {
 JPG = 0,
 PNG = 1,
 GIF = 2,
 PVR = 3
 } kImageType;

      



or even

typedef enum {
 JPG = 100,
 PNG = 0x01,
 GIF = 100,
 PVR = 0xff
 } kImageType;

      

whatever you want, duplicate values ​​are fine too.

I'm not sure why they are given those specific values, but they might have some use-related meaning.

+2


source


Usually, when you create an enum, you want to use it as a type (variable, method parameters, etc.).

In this case, it's just a way to declare integer constants. Since thay doesn't want to use enum as a type, no name is required.



Edit: Hexadecimal numbers are commonly used when the integer is a binary mask. You won't see any operators like +, -, *, / used with that number, you will see bitwise operators (!, &, |, ^).

Each digit in a hexadecimal number represents 4 bits. This is all a 32-bit integer, in which case, by writing it in hex, you are saying you only use the last four bits, and the rest of the bits can be used for something else. This would not be obvious from the decimal number.

+3


source


Well, it looks like you are working with a terrible example. :)

At least as far as transfers are concerned. Anyone can figure out the actual value of the enum entry, but there is no benefit to using hex numbers and in particular there is no point in running hex numbers with a through f (10 to 15). An example will work with this enum as well:

enum {  
easySprite = 10,
mediumSprite,
hardSprite,
backButton,
magneticSprite,
magneticSprite2
};

      

And if it doesn't make any sense to start the enumeration with a value of 10, it will probably work without specifying any specific values.

+1


source







All Articles