Latest 1.0.1
Homepage https://github.com/nicholascross/StateTransition
License MIT
Platforms osx 10.9, ios 9.0, tvos 9.0, watchos 2.0, requires ARC

build status
code coverage
carthage compatible
CocoaPods compatible
GitHub release
Swift 3.0.x
platforms

A swift state machine supporting; states, transitions, actions and transition handling

        enum StateOfMatter {
            case Solid
            case Liquid
            case Gas
            case Plasma
        }

        enum EnergyTransfer {
            case Increase
            case Decrease
        }

        var stateMachine = StateMachine<EnergyTransfer, StateOfMatter, String>(initialState: .Solid)

        stateMachine.addTransition(fromState: .Solid, toState: .Liquid, when: .Increase)
        stateMachine.addTransition(fromState: .Liquid, toState: .Gas, when: .Increase)
        stateMachine.addTransition(fromState: .Gas, toState: .Plasma, when: .Increase)
        stateMachine.addTransition(fromState: .Plasma, toState: .Gas, when: .Decrease)
        stateMachine.addTransition(fromState: .Gas, toState: .Liquid, when: .Decrease)
        stateMachine.addTransition(fromState: .Liquid, toState: .Solid, when: .Decrease)

        func stateOfMatterChanged(energyTransfer: EnergyTransfer, fromState: StateOfMatter, toState: StateOfMatter, context:String?) {
            print("transitioned from (fromState) to (toState) as result of energy (energyTransfer)")

            if let c = context {
                print("(c)")
            }
        }

        stateMachine.addHandlerForTransition(toState: .Liquid, handler: stateOfMatterChanged)
        stateMachine.addHandlerForTransition(toState: .Gas, handler: stateOfMatterChanged)
        stateMachine.addHandlerForTransition(toState: .Plasma, handler: stateOfMatterChanged)

        stateMachine.addHandlerForTransition(toState: .Liquid) {
            _, fromState, _, _ in
            if fromState == .Solid {
                print("it melted.")
            }
        }

        stateMachine.perform(action: .Increase)
        //prints: transitioned from Solid to Liquid as result of energy Increase
        //prints: it melted.
        stateMachine.perform(action: .Increase)
        //prints: transitioned from Liquid to Gas as result of energy Increase
        stateMachine.perform(action: .Increase, withContext: "it is very hot")
        //prints: transitioned from Gas to Plasma as result of energy Increase
        //prints: it is very hot

Latest podspec

{
    "name": "StateTransition",
    "version": "1.0.1",
    "summary": "A swift state machine supporting; states, transitions, actions and transition handling",
    "homepage": "https://github.com/nicholascross/StateTransition",
    "license": "MIT",
    "authors": "Nicholas Cross",
    "platforms": {
        "osx": "10.9",
        "ios": "9.0",
        "tvos": "9.0",
        "watchos": "2.0"
    },
    "source": {
        "git": "https://github.com/nicholascross/StateTransition.git",
        "tag": "1.0.1"
    },
    "source_files": "StateTransition/*.swift",
    "requires_arc": true,
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This