Austin Rude's Blog

Programming with Swift, iOS, macOS, etc.

State machines in Swift

2016-09-25 Programming
The problem You have a large and complicated if/switch statement You keep copying parts of the if/switch block around the codebase You keep needing to add/tweak the conditions for edge cases If you run into issues like these, it might be a valid use case for a finite-state machine. A state machine can help you simplify the code, and make the logic more explicit! My experience of the issue When working on my app Nap Slide I ran into an issue handling transitions between alarm clock states. Continue reading