Latest 0.2.1
Homepage https://github.com/cipriancaba/SwiftFSM
License MIT
Platforms ios 8.0, requires ARC
Authors

CI Status
Version
License
Platform

A solid yet simple fsm implementation in Swift

Features

  • [x] Type safety with Hashable enums
  • [x] Simple configuration with Enum based States and Transitions
  • [x] Type-safety for States and Transitions using Generics
  • [x] Convenient state handling with closures for onEnter and onExit
  • [ ] Delegate implementation for a more generic state handling
  • [ ] CocoaPods and Carthage support

Requirements

  • iOS 8.0+ / Mac OS X 10.9+
  • Swift
  • Xcode 6.4

Installation

CocoaPods

To integrate SwiftFSM into your Xcode project using CocoaPods, specify it in your Podfile:

platform :ios, '8.0'
use_frameworks!

pod 'SwiftFSM'

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

  • [ ] Detailed instructions to be added once Carthage is supported

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate SwiftFSM into your project manually by simply copying SwiftFSM.swift and SwiftFSMState.swift into your project

Usage

Classic turnstile example project

To run the example project, clone the repo, and run pod install from the Example directory first.

Defining a state

enum TurnstileState: String {
  case Locked = "Locked"
  case Unlocked = "Unlocked"
}

Defining a transition

enum TurnstileTransition: String {
  case Push = "Push"
  case Coin = "Coin"
}

Defining the actual fsm

This is the place where you need to define the Hashable State and Transition generics. This will help Xcode determine any compile time errors

You will need to specify the id of the fsm (in case you want to use multiple fsms and log their events)

This is the place where you can enable or disable fsm logging

let fsm = SwiftFSM<TurnstileState, TurnstileTransition>(id: "TurnstileFSM", willLog: false)

Defining a state

Any state you define must be unique
You will receive a reference to the actual SwiftFSMState implementation when calling fsm.addState

This reference is needed for defining the transitions from that state and for mapping the state handlers

let locked = fsm.addState(.Locked)

// Define an inline onEnter handler
locked.onEnter = { (transition: TurnstileTransition) -> Void in
  // called when the locked state defined above is entered. You also receive the type of transition that generated the state change
}

let unlocked = fsm.addState(.Unlocked)

// Define handlers as reference
unlocked.onEnter = handleUnlocked
// Listen to onExit event
unlocked.onExit = handleUnlockedExit

Defining a transition

You obviously need to define transitions between states and in SwiftFSM this comes really natural

locked.addTransition(.Push, to: .Locked)
locked.addTransition(.Coin, to: .Unlocked)

unlocked.addTransition(.Coin, to: .Unlocked)
unlocked.addTransition(.Push, to: .Locked)

Starting the fsm

Once you are happy with the configuration of the fsm, you will need to start it

Please note that this will not trigger the initial onEnter callback

fsm.startFrom(.Locked)

Check if a transition was successful

if let newState = fsm.transitionWith(.Coin) {
  // state change happened
} else {
  // transition was not valid
}

Current state

You can access the current state of the fsm at any moment. This will return a State

fsm.currentState

Author

Ciprian Caba, [email protected]

License

SwiftFSM is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SwiftFSM",
    "version": "0.2.1",
    "summary": "A solid yet simple fsm implementation in Swift",
    "description": "Finit state machine implementation using Enums, Generics and Closures",
    "homepage": "https://github.com/cipriancaba/SwiftFSM",
    "license": "MIT",
    "authors": {
        "Ciprian Caba": "[email protected]"
    },
    "source": {
        "git": "https://github.com/cipriancaba/SwiftFSM.git",
        "tag": "0.2.1"
    },
    "social_media_url": "https://twitter.com/cipriancaba",
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "Pod/Classes/**/*"
}

Pin It on Pinterest

Share This