【发布时间】:2019-04-18 23:02:42
【问题描述】:
我的 Playground 中有以下代码:
import GameplayKit
class TestClass {
var sm: GKStateMachine
init() {
sm = GKStateMachine(states: [MyState()])
sm.enter(MyState.self)
}
}
class MyState: GKState {
override init() {
super.init()
}
override func didEnter(from previousState: GKState?) {
printStateM()
}
func printStateM() {
if (self.stateMachine == nil) {
print("StateMachine")
} else {
print("No StateMachine")
}
}
}
var t = TestClass()
输出是“无状态机”。 我想知道为什么 MyState 的 StateMachine 属性是 nil?
【问题讨论】:
-
不应该是
self.stateMachine != nil吗? (或者如果您打算使用该变量,您可能需要if let stateMachine = self.stateMachine或类似的东西)
标签: swift gameplay-kit