我刚刚将我的一个项目更新到 Swift 5 并且发生了一件很奇怪的事情。我正在使用 aGKStateMachine
来处理游戏的状态。进入状态时,我还将枚举的值设置为相关案例,以便与外部进行更清晰的通信。现在更新到 Swift 5 后,它不再正常工作了。
一个例子
这是枚举:
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
}
现在在状态机的 GameOver 状态中,我执行以下操作:
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
default:
return false
}
}
}
它进入状态,在 »didEnter(from previousState: GKState?)« 方法中,我将 GameManager 对象的 currentState 设置为 .gameOver - 到目前为止一切顺利。但不知何故,这不起作用,因为它被设置为 .lostTrack 。.paused 情况下的 Paused State 也会发生同样的情况。
有趣的是,当我更改枚举中案例的顺序时,它也会将其设置为另一个案例。除了 .paused 和 .gameOver 之外,所有其他的似乎都可以正常工作并正确设置。这在以前肯定有效,我不知道会发生什么。在这一点上,在我看来就像一个系统错误
Sidefact:当我摆脱枚举案例的相关值时,它也可以工作。
Xcode 10.2 和 Xcode 10.2.1 的结果相同
重现问题的示例 Playground: https ://www.sendspace.com/file/qudvzt