初始化后关闭意味着什么?

时间:2018-12-14 20:15:35

标签: swift syntax

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带有结尾的结尾。 init中没有定义任何GameScene。那么GameScene(fileNamed: "GameScene")之后的闭包是什么?

1 个答案:

答案 0 :(得分:4)

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

有关更多信息,请参见《 Swift语言指南》的Optional Binding部分。

相关问题