Thread 1: Fatal error: init(coder:) has not been implemented

Good day, I am studying GKEntities and GKComponent from the book first version I think that has the chapter.
Page 478, Xcode 7 and Swift 2.

Problem:

Xcode: required init?(coder aDecoder: NSCoder) {
fatalError(“init(coder:) has not been implemented”)
}
//Thread 1: Fatal error: init(coder:) has not been implemented

Xcode forces to have this code and it fails every time it is called even using a second custom init().

Questions:

How to avoid using this required init that is everywhere with GKComponents and use normal inits instead to initialize the GKComponent variables.

Note: For all readers of this post I must say my question is not an error on the Book, but help with the complexities of Xcode. But I would appreciate any help.


import SpriteKit

import GameplayKit

class EntityNode : SKSpriteNode {

weak var entity: GKEntity ! //Xcode says there is already a variable with this name.

}

class SpriteComponent : GKComponent {

// A node that gives an entity a visual sprite

let node: EntityNode

init (entity: GKEntity , texture: SKTexture , size: CGSize ) { //I expect this to be called

node = EntityNode (texture: texture,

color: SKColor .whiteColor(), size: size)

node.entity = entity

}

//Xcode forces to use this but fails always.
required init?(coder aDecoder: NSCoder) { //Instead calls this required init
fatalError(“init(coder:) has not been implemented”)
}
//Thread 1: Fatal error: init(coder:) has not been implemented
//It should call instead init (entity: GKEntity , texture: SKTexture , size: CGSize ) that was the one used.

}

Where is it called? You don’t show any code creating a new instance of SpriteComponent.

First the GKComponent: This is the error I get when running the component. The code is just for an elipse to represent a shadow under a sprite. Taken from the book but xcode complains with missing “required init” that then fails.

Second the GKEntity: Init the shadow and add it to the GKEntity as a component using default values. here show I am forced to use the another “required init”

Third Scene Editor, I hook up the Entity to a SKSpriteNode in the Scene Editor.

image

and shadowComponent

image

Finally, In the Scene.swift I try to attach the Shadow to the sprite.

and It does not work because

a. the Required Init error
b. Me trying to use the scene Editor instead of doing all by code as done in the book.

But I am trying to find the solutions step by step first with point a and then for b.

image

If you use these classes in Scene Editor, they are going to use the init(coder:) initializer. That’s how it creates them from the properties you set in the Editor.

So you need to put some code into the init(coder:) initializer, not create a different one. You can call super.init(coder:) inside it. Then add the additional initialization you need.

Thank you,:grinning: I will try your sugestion putting the initialization code inside this required init. I hope this will works :crossed_fingers:.

This topic was automatically closed after 166 days. New replies are no longer allowed.