How do you create enums for state charts in ObjC?

Member
Posts: 249
Joined: 2008.10
Post: #1
Hi mates!

How do you create enums for state charts in ObjC?
Usually, in C++ I do something like:

enum GAME_STATE {
STARTING,
PLAYING,
...
ENDING
};
GAME_STATE m_game_state;

But it seems "enum" does not exist in ObjC.
How do you usually implement state charts or finite state machines in ObjC? I mean, How do you exactly represent states?

Thanks a lot.
Quote this message in a reply
Moderator
Posts: 702
Joined: 2002.04
Post: #2
enum's exist in C, and as Objective-C is a strict superset of C they exist in that also; the following compiles happily in Objective-C:

Code:
typedef enum
{
STARTING,
PLAYING,
ENDING
}
GAME_STATE;

[i][snip][/i]

GAME_STATE m_game_state;

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #3
sealfin Wrote:enum's exist in C, and as Objective-C is a strict superset of C they exist in that also; the following compiles happily in Objective-C:

Code:
typedef enum
{
STARTING,
PLAYING,
ENDING
}
GAME_STATE;

[i][snip][/i]

GAME_STATE m_game_state;

Thanks for reply.
Just one question, in my C++ programs I don't use typedef before enum, why do I have to use typedef if C has enums?

Thanks.
Quote this message in a reply
Moderator
Posts: 702
Joined: 2002.04
Post: #4
Because C++ is not a strict superset of C: link. Rasp

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Member
Posts: 249
Joined: 2008.10
Post: #5
sealfin Wrote:Because C++ is not a strict superset of C: link. Rasp

Thanks!

I guess you are talking about:
"A struct, union, or enum declaration in C++ is a first class type, while in C it is not."
isn't it?
Quote this message in a reply
Member
Posts: 241
Joined: 2008.07
Post: #6
This is interesting stuff. I had a similar problem, except it would just give me warnings saying that the statement did not declare anything. This happened when trying to declare the enum in the scope of a class interface. Simply moving it to global scope fixed the problem. Also, when declaring instances of the enum, I always had to put the "enum" before the declaration. For example:

Code:
@interface Whatever
{
     enum ENUM {ZERO, ONE, TWO, ETC}; // Gives warning.
}

@end

Code:
enum ENUM {...}; // No problem.

@interface Whatever
{
     enum ENUM m_enum; // Errors without the "enum" at the beginning.
}

@end

Any thoughts?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
Instance variables go between those braces, not any old declaration you like.

In C, "struct S { ... }" declares a type called "struct S", and "enum E { ... }" declares a type called "enum E". In C++, "struct S { ... }" declares a type with two names, "struct S" and "S", and "enum E { ... }" declares a type with two names, "enum E" and "E". To get the simpler name like C++ in C you need a typedef, for example "struct S { ... }; typedef struct S S;", or you can shorten that to "typedef struct S { ... } S;".
Quote this message in a reply
Moderator
Posts: 3,579
Joined: 2003.06
Post: #8
Or if you don't care about having a custom type you can just do (in C):

enum {
STARTING,
PLAYING,
ENDING
};
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Game State Management Mattonaise 6 7,307 Apr 16, 2011 10:18 AM
Last Post: Mattonaise
  Taking Pictures of Game State inside an app Bersaelor 7 5,433 Nov 3, 2009 05:07 AM
Last Post: Bersaelor
  objc++ problem vidjogamer 7 3,327 Aug 5, 2009 02:02 PM
Last Post: longjumper
  Save game state using Property list or Text File? Graphic Ace 2 3,687 Apr 6, 2009 03:48 AM
Last Post: Graphic Ace
  Saving the game state when pressing the Home button Nacho 3 3,417 Feb 10, 2009 05:33 PM
Last Post: Nacho