Swift <uninitialized> 让 属性

Swift <uninitialized> let property

我需要一些调试帮助,因为我 运行 遇到一个错误真的很难。

这是一款动画复杂的游戏。然而,问题不在于 SpriteKit。我希望动画按照严格的顺序相互跟随,所以我实现了 Operation:

的 subclass
class ActionOperation: Operation
{
    var debugLabel: String?

    private(set) var actionNodes: Set<ActionNode>

    //... Other vars, isFinished etc.

    init(node: SKNode, action: SKAction) {
       actionNodes = [ActionNode(node: node, action: action)]
       super.init()
    }

    init(nodesAndActions: [(SKNode?, SKAction)]) {
        actionNodes = Set(nodesAndActions.map( {(tuple) in return 
            ActionNode(node: tuple.0, action: tuple.1)
        }))
        super.init()
    }

    override func start() { /* ... */ }
}

对于此 class 详细信息,您可以查看 。 辅助结构:

extension ActionOperation {
    struct ActionNode: Hashable {
        static func ==(lhs: ActionOperation.ActionNode, rhs: ActionOperation.ActionNode) -> Bool {
            return lhs.node == rhs.node && lhs.action == rhs.action
        }

        weak var node: SKNode?

        // This constant is causing problems!
        let action: SKAction

        let setIdForHash: Int

        var hashValue: Int { return setIdForHash ^ action.hashValue }
    }
}

问题

ActionOperation 的实例已添加到 animationQueue。队列设置:

fileprivate let animationQueue = OperationQueue()

// Setup:
animationQueue.maxConcurrentOperationCount = 1
animationQueue.qualityOfService = .userInteractive

操作设置:

let duration = 0.35
var groupActions = [SKAction]()
for n in 0..<from.count {
    let fromIndex = from[n]
    let toIndex = to[n]
    let move = SKAction.move(to: fieldPosition(at: toIndex), duration: duration)
    let seq = SKAction.sequence([
        SKAction.run(move, onChildWithName: "\(fromIndex)"),
        SKAction.wait(forDuration: duration),
        SKAction.run({
            self.piece(at: fromIndex)?.name = "\(toIndex)"
        })
    ])
    groupActions.append(seq)
}
let gravOperation = ActionOperation(node: piecesLayer, action: SKAction.group(groupActions))
gravOperation.debugLabel = "gravity"
animationQueue.addOperation(gravOperation)

有时这个队列会得到堆栈,这意味着一个操作永远在执行。我正在尝试使用 Xcode 命令行调试它并输入:

p (animationQueue.operations[0] as! ActionOperation).actionNodes.first!.action

(SKAction) $R6 = <uninitialized>

这是什么意思? 结构中的 let 常量如何成为 uninitialized

如果我打印node,一切正常:

po (animationQueue.operations[0] as! ActionOperation).actionNodes.first!.node

<SKNode> name:'(null)' position:{0, 0} scale:{1.00, 1.00} accumulatedFrame:{{178.13499450683594, -26.469999313354492}, {908.33502197265625, 1012.9400024414062}}

嗯,问题是我 crate group action 的方式引起的:

let gravOperation = ActionOperation(node: piecesLayer, action: SKAction.group(groupActions))

groupActions 数组为空时,将永远不会在节点上执行操作,如果我们调用:

node.run(badGroupAction, completion: { /* never called */ })

completion 永远不会被调用。这就是队列卡住的原因。我认为这是一个 SpriteKit 缺陷。如果我们尝试 运行 清空组或序列,我希望立即调用 completion

为什么action变成了uninitialized?不知道,我觉得是个bug,可能跟空数组有关