StateMachine
State machine creation framework written in Swift inspired by GKStateMachine from Apple GameplayKit
Install / Use
/learn @alexruperez/StateMachineREADME

StateMachine
Swift library to create Finite-state machines inspired by GKStateMachine from Apple GameplayKit framework.
Test StateMachine working live!
🌟 Features
- [x] Define States and Their Behavior.
- [x] Create and Drive a State Machine.
- [x] Subscribe/unsubscribe to State changes.
- [x] UIApplication and UIApplicationDelegate extensions with application life cycle State Machine embedded.
- [x] StatefulViewController subclass with UIViewController life cycle State Machine embedded.
📲 Installation
StateMachine is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'ArchitStateMachine'
Or you can install it with Carthage:
github "alexruperez/StateMachine"
Or install it with Swift Package Manager:
dependencies: [
.package(url: "https://github.com/alexruperez/StateMachine.git")
]
🐒 Usage
Define States and Their Behavior:
class MyState: State {
func isValidNext<S>(state type: S.Type) -> Bool where S : State {
switch type {
case is OneValidNextState.Type, is OtherValidNextState.Type:
return true
default:
return false
}
}
}
class OneValidNextState: State {
func isValidNext<S>(state type: S.Type) -> Bool where S : State {
return type is OtherValidNextState.Type
}
func didEnter(from previous: State?) {
// Your code here
}
}
class OtherValidNextState: State {
func isValidNext<S>(state type: S.Type) -> Bool where S : State {
return false
}
func willExit(to next: State) {
// Your code here
}
}
Create and Drive a State Machine:
let stateMachine = StateMachine([MyState(), OneValidNextState(), OtherValidNextState()])
stateMachine.enter(OneValidNextState.self)
stateMachine.enter(OtherValidNextState.self)
Subscribe/unsubscribe to State changes:
let subscriptionToken = stateMachine.subscribe { (previous, current) in
// Your code here
}
stateMachine.unsubscribe(subscriptionToken)
stateMachine.unsubscribeAll()
UIApplication and UIApplicationDelegate extensions with application life cycle State Machine embedded:

if UIApplication.shared.stateMachine.current is ActiveApplicationState {
// Your code here
}
StatefulViewController subclass with UIViewController life cycle State Machine embedded:

let viewController = StatefulViewController()
if viewController.stateMachine.current is AppearingViewControllerState {
// Your code here
}
❤️ Etc.
- Contributions are very welcome.
- Attribution is appreciated (let's spread the word!), but not mandatory.
👨💻 Authors
alexruperez, contact@alexruperez.com
👮♂️ License
StateMachine is available under the MIT license. See the LICENSE file for more info.
Related Skills
node-connect
343.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
92.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.3kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
