gpt4 book ai didi

swift - 如何覆盖 SKScene init(fileNamed : fileNamed)

转载 作者:搜寻专家 更新时间:2023-11-01 05:36:39 39 4
gpt4 key购买 nike

我有一个 SKScene 类,我需要在其中实现一个自定义初始化程序来覆盖 SKScene 父类(super class) SKNode 的初始化程序 init(fileNamed: fileNamed),以便进行一些专有初始化,但保留 init(fileNamed: fileNamed) 从界面构建器加载场景。

我很难找到正确的语法。我尝试了以下方法:

class Try: SKScene
{
override init(fileNamed: String)
{
super.init(fileNamed: fileNamed)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

但它返回错误:Initializer does not override a designated initializer from its superclass

然而,它是来自父类(super class)的指定初始化器,不是直接父类(super class) SKEffectNode,而是上面的父类(super class) SKNode。在这种情况下,我该如何覆盖初始化程序?

J.

最佳答案

来自SWIFT docs :

Conversely, if you write a subclass initializer that matches a superclass convenience initializer, that superclass convenience initializer can never be called directly by your subclass, as per the rules described above in Initializer Delegation for Class Types. Therefore, your subclass is not (strictly speaking) providing an override of the superclass initializer. As a result, you do not write the override modifier when providing a matching implementation of a superclass convenience initializer.

如果我理解正确,这段代码应该可以工作:

convenience init?(fileNamed filename: String)
{
self.init(fileNamed: filename)
}

关于swift - 如何覆盖 SKScene init(fileNamed : fileNamed),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37488568/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com