init之后的闭包是什么意思



Xcode游乐场游戏样板生成代码,带有类:

class GameScene: SKScene {
// no init override here
}

然后实例化类:

if let scene = GameScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
sceneView.presentScene(scene)
}

我搜索了文档,没有带尾随闭包的init。CCD_ 3中未定义任何CCD_。那么GameScene(fileNamed: "GameScene")之后的闭包是什么呢?

这不是闭包,而是可选的绑定。GameScene(fileNamed:)是一个失败的初始值设定项,因此可能返回nil。可选的if let绑定返回值,这意味着如果返回值不是nil,则会命中if分支,并且在if语句中,scene保证为非零。

有关更多信息,请参阅Swift语言指南的可选绑定部分。

最新更新