Swift Backend#
Swift is a modern object-oriented programming language for iOS and macOS development.
To generate Swift code, call the code generator with the command line flag -l swift.
To generate a configuration file with all Swift-related parameters, run once:
java -jar codegen.jar -l swift -gencfg > codegen.cfgEvents that can be sent to the state machine are generated as a public enumeration.
The generator creates one Swift class that implements the complete state machine. This keeps your Swift project compact by avoiding many helper classes. The generated code does not follow the classic state pattern because the machine code is fully generated and no hand-coding is required.
Separate Generated and Non-Generated Code#
Even when the state machine is fully generated, it is usually only a part of your application. The larger part is often manually coded. It is important to keep generated and non-generated code clearly separated.
You can do this in different ways:
- Put hand-written code into libraries and call those libraries from within the state machine.
- Let generated classes subclass non-generated classes (for example a base class of the state machine). Base classes can provide helper methods used by generated subclasses.
- Place hand-written code in child classes of the state machine (state machine classes become parts of other classes).
Supported State Machine Features#
- States and sub-states
- Deep and flat hierarchy
- Entry, exit, and action code of states
- Regions (implemented as sub-functions called from the main state machine handler)
- Option to define state machine signature
- Choice pseudo-states
Example Design and Generated Code#

Download generated Swift example code
Please note that the Swift backend is work-in-progress.
If you are interested in testing and using the code generator, send an email to request the latest beta version.
Feedback and feature requests are highly welcome.