Enums in Objective-C? - Printable Version
+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: iPhone, iPad & iPod Game Development (/forum-11.html)
+--- Thread: Enums in Objective-C? (/thread-1863.html)
Enums in Objective-C? - demonpants - Jan 23, 2009 01:50 PM
This is a syntax-based question. I'm having a bit of trouble getting enums working in Objective-C. I'm also not sure where the best place to declare them is, but within @implementation seems like it would be fine.
That's what I've been trying. The enum declaration works fine, but neither of the two assignments in doStuff are legal. Can someone just write out the proper way of doing this? Thanks.
Enums in Objective-C? - ThemsAllTook - Jan 23, 2009 02:22 PM
enum in C (and thereby Objective-C) is simply syntactic sugar for #define, plus a little bit of type safety. The way you're written that code, "enum animalTypes" and "Animals" are both integer types that will require all values thereof to be handled in any switch statement on variables of that type. DOG, CAT, MONKEY, and MONGOOSE are all constants that can be used directly without any prefix.
Enums in Objective-C? - Blacktiger - Jan 23, 2009 04:40 PM
You can actually put your enums just about anywhere you want. In your example, you could put the enum outside of the implementation or inside it, but I would keep it in the .m file since it's just a part of the implementation details. My personal style would be to list it before the @implementation keyword.
If you were going to use an enum to define some constants to use a parameters to a method you should put it in the header file instead so clients can just import the header. In that case I would highly recommend keeping the enum outside of the @interface declaration. I'm not certain that it would matter syntactically, but it makes the most sense semantically.
Enums in Objective-C? - kalimba - Jan 23, 2009 06:40 PM
So, the gist is that the OP should just do the following, correct?