Swift/SpriteKit 如何检查SKNode实例是否不为nil

Swift/SpriteKit How to check whether SKNode instance is not nil

为什么下面的代码不起作用?

if let helloNode: SKNode = self.childNodeWithName("helloNode")! { ... }

self.childNodeWithName("helloNode") returns SKNode?.

!将 return 值解包到 SKNode。 helloNode 捕获 SKNode.

我做错了什么?

你不想打开它。

if let helloNode: SKNode = self.childNodeWithName("helloNode") { ... }

if let的重点是在进入区块之前先看看值是否不为nil。如果你在 'if let' 语句中解包它,它就达不到目的了。

删除“!”。它不适合 "if let .." 上下文。 "If let ..." 已经展开值。