How to Make a Basic Finite State Machine in Objective-C How to Make a Basic Finite State Machine in Objective-C objective-c objective-c

How to Make a Basic Finite State Machine in Objective-C


I suggest using State Machine Compiler, it will output Objective-C code. I have had good success in Java and Python using this.

You shouldn't be writing state machine code by hand, you should be using something to generate the code for you. SMC will generate clean clear code you can then look at if you want to learn from it, or you can just use it and be done with it.


When you use a protocol as a type-modifier, you can provide a comma-separated list of protocols. So all you need to do to get rid of the compiler warning is add NSObject to the protocol list like so:

- (void)setupTimer:(id<TimerState,NSObject>) timerState {    // Create scheduled timers, etc...}


If you want a very simple, Objective-C implementation of a State Machine I've just released TransitionKit, which provides a well designed API for implementing state machines. It's thoroughly tested, well documented, very easy to use, and doesn't require any code generation or external tools.