Thanks,
I found an about.com article about enums{}, but the example Xcode project I'm looking at has only one constant in it, so what is the point of using an enum{} here?
enum {
kTagTileMap = 1,
};
Code:enum { kTagTileMap = 1, };
Here is the tutorial I'm following:
http://paulsonapps.wordpress.com/2010/03/12/tutorial-1-tilemap-with-collision-game-cocos2d/
enum RGBColor { RedColor, GreenColor, BlueColor };
#define kTagTileMap 1
The second way is to create constants without using the evil of #define. The old-school way of doing the code you posted is:
Code:#define kTagTileMap 1
So what's evil about #define? I've never really thought about it, and am open to conversion on this issue.
-(void)kTagTileMap
{
return 1;
}
The pre-processor doesn't consider syntactic context at all. It just blindly replaces tokens.
There are two modern uses of enum.
The first, and its original, usage is what the about.com "article" probably talked about, which is creating enumeration types. For example:
Code:enum RGBColor { RedColor, GreenColor, BlueColor };
I would think of that as a rather poorly named example. RGBColor SOUNDS like a struct of 3 distinct parts. Perhaps named as PrimaryColor or something it would make more sense.
Something to think about.
Also, for modern code I think it's better to use the const keyword than enum if what you really want is a constant, if you don't want to use the #define that is.
#include "makesound.h"
int main(int argc, const char* argv[])
{
makeSound(kBeep);
makeSound(kBoop);
makeSound(-1);
return 0;
}
#ifndef makesound_h
#define makesound_h
const int kBeep = 1;
const int kBoop = 2;
void makeSound(int soundConstant);
#endif
#include "makesound.h"
#include <stdio.h>
void makeSound(int soundConstant)
{
switch (soundConstant) {
case kBeep:
printf("Beep\n");
break;
case kBoop:
printf("Boop\n");
break;
default:
fprintf(stderr, "%s: invalid sound constant: %d\n", __PRETTY_FUNCTION__, soundConstant);
break;
}
}
ld: duplicate symbol _kBeep in makesound.o and main.o