My understanding from the Swift 4.2 Guide is that designated initialisers must initialise all stored properties. Convenience initialisers can initialise a subset of properties (which may be useful in creating instances under different circumstances) however, they must first call a designated initialiser of the subclass that in turn calls the designated initialiser of the superclass(es) to initialise all stored properties before customisation begins. This diagram from the Swift Guide may help:
By using the convenience keyword Leo can then call self.init() which is PreviewNode's designated initialiser which in turn calls SCNNode's designated initialiser. That sorts out the error we both got: 'required' initializer 'init(coder:)' must be provided by subclass of 'SCNNode'. That's because UIView adopts the NSCoding protocol.
Below is a commented version of Leo's code that shows the three options for handling the problem. I think they are all equally correct, but one may suit a particular problem better than others.
import SceneKit
class PreviewNode: SCNNode {
let previewNodeColor: UIColor = .red
let size: CGFloat = 1
let chamferRadius: CGFloat = 0
//convenience override init() { //Option 1 - use with self.init() to use the default designated initialisers up the inheritance ladder
//override init() { //Option 2 - use with super.init() and the additional required initialiser below
required init(coder aDecoder: NSCoder) { //Option 3 - Use with super.init() to initialise the geometry property of SCNNode and the required intialiser
//self.init()
super.init()
let previewBox = SCNBox(width: size, height: size, length: size, chamferRadius: chamferRadius)
previewBox.firstMaterial?.diffuse.contents = previewNodeColor
previewBox.firstMaterial?.transparency = 0.2
previewBox.firstMaterial?.specular.contents = UIColor.white
self.geometry = previewBox
}
//Use with Option 2 to provide the additional required initialiser for NSCoder
/*
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
*/
}
Sorry to bump an old post but I got the same problem and the research was fun :-)