I just updated one of my projects to Swift 5 and have a pretty weird occurrence. I'm using a GKStateMachine to handle states for a game. When entering a state I also set the value of an enum to the related case for cleaner communication to the outside. Now after the update to Swift 5 this isn't properly working anymore.

An example

This is the enum:

public enum GameState: Equatable {
    case setup
    case preparing
    case calibrating(calibrationState: GameCalibrationState)
    case lostTrack
    case readyToPlay
    case running(canSkipItems: Bool)
    case handlingTask
    case paused
    case gameOver

Now in the GameOver State of the State Machine I do the following:

class GameOverState: GKState {
    weak var gameManager: GameManagable?

    init(gameManager: GameManagable) {
        self.gameManager = gameManager

    override func didEnter(from previousState: GKState?) {
        // Entered game over state and now trying to set game manager state to .gameOver:
        gameManager?.currentState = .gameOver
        // The currentState property state should now be set to .gameOver but is .lostTrack instead

    override func isValidNextState(_ stateClass: AnyClass) -> Bool {
        switch stateClass {
        case is GameSetupState.Type:
            return true
            return false

It enters the state and in the »didEnter(from previousState: GKState?)« method I set the currentState of my GameManager object to .gameOver – so far so good. But somehow this isn't working as it get's set to .lostTrack instead. The same is happening for the Paused State with the .paused case.

And the console output while running: console output

Interestingly when I change the order of cases in the enum it also sets it to another case. Beside .paused and .gameOver all the other ones seem to work and get set correctly. This definitely worked before and I have no idea what could be going on. At this point it seems to me like a system bug

A screenshot of the relevant definitions in the GameManager: GameManager

Sidefact: It also works when I get rid of the associated values of the enum cases.

Same outcome in both Xcode 10.2 and Xcode 10.2.1

Example Playground that reproduces the issue: https://www.sendspace.com/file/qudvzt

0 Answers