在 Swift 中扩展 SCNNode [英] Extending SCNNode in Swift
问题描述
我想在 Swift 中扩展 SCNNode(如果可能).这是我的尝试:
I would like to extend SCNNode (if possible) in Swift. Here's my attempt:
class MyNode : SCNNode {
override init(geometry: SCNGeometry) -> SCNNode {
return super.init(geometry)
}
/* Xcode required this */
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
-> 操作符被标记为错误一行中的连续声明必须用 ; 分隔".
The -> operator is being tagged with an error "Consecutive declarations on a line must be separated by ;".
我知道这是不对的,但如果我幽默 Xcode,它会按如下方式修复"它:
I know this is not right but if I humour Xcode it "fixes" it as follows:
override init(geometry: SCNGeometry); -> SCNNode {
这是无稽之谈,然后Xcode抱怨预期声明".
Which is nonsense and then Xcode complains "Expected declaration".
我不太了解 SCNNode 的实现——如果我在 Xcode 中查看它,它都被声明为抽象的(在注释中)并且不提供任何实现.文档没有建议从 SCNNode 扩展任何东西,你直接实例化它,所以我假设我应该能够扩展它.
I don't quite understand the SCNNode implementation - if I look at it in Xcode it's all declared as abstract (in comments) and offers no implementation. The docs do not suggest anything extends from SCNNode, you instantiate it directly, so I am assuming I should be able to extend it.
推荐答案
您只需:
class MyNode : SCNNode {
/* Xcode required this */
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
哪个将从基类继承指定的初始化器.
Which will inherit the designated initialiser from the base class.
如果您想指定自己的带有几何图形的初始化程序,则必须调用基类的指定初始化程序,所以我会说这就是您想要的:
If you want to specify your own initialiser that takes a geometry, you must call a designated initialiser of the base class, so I'd say this would be what you'd want:
class MyNode : SCNNode {
init(geometry: SCNGeometry) {
super.init()
self.geometry = geometry
}
/* Xcode required this */
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
您在 SCNNode.h
代码中看到的初始化程序是从 Objective C 方法 nodeWithGeometry
翻译(我相信是自动的)的初始化程序.我相信这是一个方便的初始化程序,即使它没有特别标记为这样,我个人认为这是自动头文件翻译的错误/限制.文档 特别指出:
The initialiser you're seeing in the SCNNode.h
code is an initialiser translated (automatically, I believe) from the Objective C method nodeWithGeometry
. I believe this is a convenience initialiser, even though it's not specifically marked as such, which I'd personally say is a bug/limitation of the automatic header file translation. The documentation specifically says:
为了一致性和简单性,Objective-C 工厂方法被映射为 Swift 中的便利初始值设定项.
For consistency and simplicity, Objective-C factory methods get mapped as convenience initializers in Swift.
不要被注释中的abstract
这个词弄糊涂了;这是 HeaderDoc 标签,用于方法的简短描述,即抽象"用于摘要或摘要.
Don't be confused by the word abstract
in the comments; this is the HeaderDoc tag for a short description of a method, i.e. "abstract" is used in the sense of an abridgement or summary.
这篇关于在 Swift 中扩展 SCNNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!